2011-03-25 18:41:52 题目地址:http://acm.fzu.edu.cn/problem.php?pid=1061 找到运算规律,考虑到各种情况,然后才能AC之~~~+_+ #include<iostream> #include<vector> #include<string> using namespace std; struct Matrix { int row; int col; }; int main() { int n,i,c,r,len; char ch,str[100]; while (scanf("%d",&n)!=EOF) { Matrix *p1=NULL,*p2=NULL,*m = new Matrix[27]; int count=0; vector<Matrix*> vp; for(i=0;i<n;i++) { scanf(" %c%d%d",&ch,&r,&c); m[ch-'A'].row=r; m[ch-'A'].col=c; } m[26].row=-1; scanf("%s",str); len=strlen(str); for (i=0;i<len;i++) { if(str[i]=='(') { if(p1!=NULL) vp.push_back(p1); vp.push_back(m+26); p1=NULL; } else if(str[i]==')') { vp.pop_back(); if(!vp.empty()&&vp.back()->col>0) { p2=p1; p1=vp.back(); vp.pop_back(); if(p1->col!=p2->row) break; count+=p1->row*p1->col*p2->col; p1->col=p2->col; } } else { if(p1==NULL) p1=m+str[i]-'A'; else { p2=m+str[i]-'A'; if(p1->col!=p2->row) break; count+=p1->row*p1->col*p2->col; p1->col=p2->col; } } } if(i<len) printf("error/n"); else { printf("%d/n",count); } } return 0; }