#include<bits/stdc++.h>
using namespace std;
const int maxn=300;
struct node{
long long r,c;
}mt[maxn];
int n;
long long ans=0;
stack<node>stk;
string s;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
char ch;
int r,c;
cin>>ch>>r>>c;
mt[ch].r=r;
mt[ch].c=c;
}
getchar();
while(getline(cin,s)){
ans=0;
int f=0;
for(int i=0;i<s.size();i++){
if(s[i]>='A'&&s[i]<='Z'){
stk.push(mt[s[i]]);
}else if(s[i]==')'){
node a,b;
b=stk.top();
stk.pop();
a=stk.top();
stk.pop();
if(a.c!=b.r){//a的列号与b的行号不相等
f=1;
break;
}
ans+=a.r*b.c*a.c;
stk.push(node{a.r,b.c});
}
}
if(f==1){
cout<<"error"<<endl;//有不能相乘的
}else{
cout<<ans<<endl;//都能相乘
}
}
return 0;
}
求助,CE了。