较为简单的栈题
思路比较好
一次ac
1、char word ;word=A;直接 a[word]=xxxx,不用 a[‘word’]=xxxx
#include<bits/stdc++.h> using namespace std; struct aaa { int x; int y; }a[1000]; int main() { stack<aaa>k; int n; cin>>n;getchar(); for(int i=1;i<=n;i++) { char word;cin>>word; cin>>a[word].x>>a[word].y; } string s; while(getline(cin,s)) { if(s=="")continue; if(s.size()==1){printf("0 ");continue;} int sum=0;int ok=1; for(int i=0;i<s.size();i++) { if(isalpha(s[i])){k.push(a[ s[i] ]);} else if(s[i]==')') { struct aaa x1=k.top();k.pop(); struct aaa x2=k.top();k.pop(); struct aaa x3; if(x1.x!=x2.y){ok=0;printf("error ");break;} sum+=x1.x*x1.y*x2.x; x3.x=x2.x;x3.y=x1.y; k.push(x3); } } if(ok)printf("%d ",sum); } return 0; }