-
Notifications
You must be signed in to change notification settings - Fork 0
/
10906.cpp
54 lines (39 loc) · 1.15 KB
/
10906.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
ll t; cin >> t;
for(ll i = 1; i <= t; i++){
cout << "Expression #" << i << ": ";
map<string, string> hi;
ll n; cin >> n;
cin.ignore(1000, '\n');
string last;
for(ll i = 0; i < n; i++){
string str; getline(cin, str);
ll eq = str.find("=");
// extract var name
string var = str.substr(0, eq);
var.pop_back();
last = var;
string right = str.substr(eq+1);
stringstream ss(right); string temp;
string res;
while(ss >> temp){
if(isalpha(temp[0])){
res += hi[temp];
continue;
}
res += temp;
}
hi[var] = "(" + res + ")";
}
hi[last].pop_back();
cout << hi[last].substr(1) << "\n";
}
return 0;
}