Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1945

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1945

Simulator

 

By Emilio Wuerges, UFFS BR Brazil

Timelimit: 1

Centuries after the alien invasion, after mankind had been rebuild completely, a very old set of programs was discovered in an obsolete programming language, called Java++. For historical curiosity, you were assigned to understand the meaning of these programs.

Your task is to write a simulator for these programs, and as initial test, this simulator must be able to calculate the value of the last assigned variable of each program.

 

Input

 

Each input consists of 1 program. The program only contains 2 kinds of instructions. One instruction to assign a variable and one to perform a sum.

Assignment instructions have the format:

A := B

Where A is a variable name and B is a positive integer.

Sum instructions have the format:

A := B + C

Where A is a variable name and B or C are either a variable name or a positive integer.

Tokens of these programs are always separated by spaces and instructions are separated by a line break.

Only combinations of at most 8 lower case letters are considered as valid variable names.

Programs have, at most, 2000 instructions

Variables are assigned, at most, only once.

 

Output

 

Output consists of only 1 line, with only one positive integer number, containing the last value of the last variable assigned, be it by a direct assignment or by a sum.

 

 

 

Input Sample Output Sample

a := 2
b := 1
c := a + b
d := a + b

3

 

Input Sample Output Sample

a := 0
b := 1
c := a + b
d := a + b
e := a + c
f := 7

7

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <bits/stdc++.h>

using namespace std;

map < string, long long> m;

long long toint(string &a)
{
	long long ret = 0;
	for (int i = 0 ; i  <  a.size(); ++i)
	{
		ret *= 10LL;
		ret += (long long)(a[i] - '0');
	}
	return ret;
}
int main()
{
	ios_base :: sync_with_stdio(0); cin.tie(0);
	
	string in;
	string salva;
	stringstream ss;
	
	string aux, aux2, aux3, op1, op2;
	
	while (getline(cin, in))
	{
		salva = in;
		ss << in;
		
		ss >> aux;
		ss >> op1;
		ss >> aux2;
		
		if (ss >> op2)
		{
			ss >> aux3;
			
			if (isdigit(aux2[0]))
			{
				if (isdigit(aux3[0]))
				{
					long long a = toint(aux2), b = toint(aux3);
					m[aux] = a + b;
				}
				else
				{
					long long a = toint(aux2);
					m[aux] = a + m[aux3];
				}
			}
			else
			{
				if (isdigit(aux3[0]))
				{
					long long b = toint(aux3);
					m[aux] = m[aux2] + b;
				}
				else
					m[aux] = m[aux2] + m[aux3];
			}
		}
		else
			m[aux] = toint(aux2);
		ss.clear();
	}
	ss << salva;
	ss >> aux;
	cout << m[aux] << '\n';
	m.clear();
	ss.clear();
	
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
a := 2
b := 1
c := a + b
d := a + b

Output

x
+
cmd
3
Advertisements

Demonstration


Previous
#1943 Beecrowd Online Judge Solution 1943 Top N Solution in C, C++, Java, Js and Python
Next
#1953 Beecrowd Online Judge Solution 1953 Robert and Rampant RoomSolution in C, C++, Java, Js and Python