https://vjudge.net/problem/UVA-442
#include<iostream>
#include<cstdio>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 30;
int n;
struct Matrix{
int row, col;
Matrix(int x, int y):row(x), col(y){}
Matrix(){}
};
Matrix matrix[maxn];
int main()
{
while (cin >> n)
{
for (int i = 1; i <= n; i++)
{
char ch; int row, col;
cin >> ch >> row >> col;
matrix[ch-'A'] = Matrix(row, col);
}
char cmd[maxn];
while (cin >> cmd)
{
stack<Matrix> s;
int cal_t = 0;//计算次数
bool flag = false; //正误标记
int len = strlen(cmd);
for (int i = 0; i < len; i++)
{
if (cmd[i] == ')')
{
Matrix m1 = s.top(); s.pop();
Matrix m2 = s.top(); s.pop();
if (m1.row == m2.col)//注意顺序
{
cal_t += m2.row*m2.col*m1.col;
s.push(Matrix(m2.row, m1.col));//注意顺序
}
else
{
flag = true;
break;
}
}
else if (cmd[i] >= 'A' && cmd[i] <= 'Z')
s.push(matrix[cmd[i] - 'A']);
}
if (flag)
cout << "error" << endl;
else
cout << cal_t << endl;
}
}
return 0;
}