-
Notifications
You must be signed in to change notification settings - Fork 5
/
UVA-442.cpp
69 lines (65 loc) · 1008 Bytes
/
UVA-442.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<string>
#include<stack>
using namespace std;
struct Metrix {
int a, b;
};
int cal(stack<Metrix> & st) {
Metrix m1, m2;
m2 = st.top();
st.pop();
m1 = st.top();
st.pop();
if(m1.b != m2.a) {
return -1;
}
st.push({m1.a, m2.b});
return m1.a * m1.b * m2.b;
}
int main() {
int n, i, j, k;
string s;
Metrix v[28];
cin >> n;
bool flag;
while(n--) {
cin >> s;
k = s[0] - 'A';
cin >> i >> j;
v[k].a = i;
v[k].b = j;
}
while(cin >> s) {
stack<Metrix> st;
flag = true;
int sum = 0, t;
for(auto ip = s.begin(); flag && ip != s.end(); ++ip) {
if(*ip >= 'A' && *ip <= 'Z') {
st.push(v[*ip - 'A']);
}
if(*ip == ')' && st.size() > 1) {
t = cal(st);
if(t < 0) {
flag = false;
break;
}
sum += t;
}
}
while(flag && st.size() > 1) {
t = cal(st);
if(t < 0) {
flag = false;
break;
}
sum += t;
}
if(flag) {
cout << sum << endl;
} else {
cout << "error" << endl;
}
}
return 0;
}