版权声明:博主的博客不值钱随便转载但要注明出处 https://blog.csdn.net/easylovecsdn/article/details/86664330
栈模拟即可,模的我一阵酸爽,多亏vs2017强大的调试。
Code
#include <bits/stdc++.h>
#define MAX 30
using namespace std;
typedef long long LL;
struct Node {
int x, y;
Node() {}
Node(int xx, int yy) {
x = xx;
y = yy;
}
};
Node a[MAX];
stack<Node> s;
int main()
{
char c;
int n, x, y;
LL res;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> c >> x >> y;
getchar();
a[c - 'A'].x = x;
a[c - 'A'].y = y;
}
res = 0;
while ((c = getchar()) != EOF) {
if (c == '\n') {
if (res == -1)
cout << "error" << endl;
else
cout << res << endl;
while (!s.empty())
s.pop();
res = 0;
continue;
}
if (c == '(') s.push(Node(-1, 0));
else if (c == ')') {
int xx = s.top().x;
int yy = s.top().y;
s.pop();
while (xx != -1) { //代表括号"("
Node t = s.top();
s.pop();
if (t.x == -1) break;
if (t.y != xx) res = -1; //error
if (res == -1) continue;
res += xx * yy * t.x;
xx = t.x;
//yy = t.y;
}
s.push(Node(xx, yy));
}
else s.push(Node(a[c - 'A'].x, a[c - 'A'].y));
}
return 0;
}