原题链接:https://vjudge.net/problem/UVA-442
分类:栈
备注:水题
代码如下:
#include<map>
#include<stack>
#include<string>
#include<iostream>
using namespace std;
struct CP {
int m, n;
};
int main(void) {
int t, m, n;
char x;
cin >> t;
getchar();
map<char, CP>sara;
while (t--) {
cin >> x >> m >> n;
sara[x] = CP{ m,n };
getchar();
}
string s;
while (getline(cin, s)) {
int flag = 1, cnt = 0;
stack<CP>tmp;
for (int i = 0; i < s.length(); i++) {
if (s[i] == ')') {
CP t1 = tmp.top(); tmp.pop();
CP t2 = tmp.top(); tmp.pop();
if (t1.m != t2.n) {
flag = 0; break;
}
cnt += t2.m * t2.n * t1.n;
tmp.push(CP{ t2.m,t1.n });
}
else if (s[i] != '(')tmp.push(sara[s[i]]);
}
if (flag)cout << cnt << '\n';
else cout << "error\n";
}
return 0;
}