-
Notifications
You must be signed in to change notification settings - Fork 1
/
uva442.cpp
44 lines (39 loc) · 1001 Bytes
/
uva442.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
#include <iostream>
#include <stack>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
pii arr[26];
void solve(string& expr) {
stack<pii> stk;
if(expr[0] != '(') {puts("0"); return;}
ll ans = 0;
for(int i = 0; i < expr.size(); i++) {
char ch = expr[i];
if(ch == ')') {
auto b = stk.top(); stk.pop();
auto a = stk.top(); stk.pop();
stk.pop();
if(a.second != b.first) {
puts("error");
return;
} else ans += (ll)a.first * a.second * b.second;
stk.push({a.first, b.second});
} else if(ch == '(') stk.push({-1, -1});
else stk.push(arr[ch - 'A']);
}
printf("%lld\n", ans);
}
int main(void) {
int n; cin >> n;
for(int i = 0; i < n; i++) {
char a; int b, c;
cin >> a >> b >> c;
arr[a - 'A'] = {b, c};
}
string expr;
while(cin >> expr) {
solve(expr);
}
return 0;
}