标签:
这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试
得到的经验有以下几点:
像这道题主要坑在了第三点上,以后要注意避免
以下是AC代码
#include <cstdio> #include <stack> #include <cstring> #include <cctype> const int MAXN=1000+10; using namespace std; char exps[MAXN]; struct Matrix { int a, b; Matrix(int a = 0,int b = 0):a(a), b(b) {} }m[26]; stack<Matrix> s; int main(){ #ifdef DEBUG freopen("6.3.in","r",stdin); #endif int n; scanf("%d\r",&n); for(int i=0;i<n;i++){ char s0[10]; char c; scanf("%c ",&c); s0[0]=c; scanf("%d %d\r\n",&m[s0[0] -‘A‘].a, &m[s0[0] -‘A‘].b); //printf("%c %d %d\r\n",s0[0] , m[s0[0] -‘A‘].a , m[s0[0]-‘A‘].b); } while(scanf("%s",exps)==1){ int sum=0; int len=strlen(exps); int ok=1; for(int i=0;i<len;i++){ if(isalpha(exps[i])){ s.push(m[exps[i]-‘A‘]); // printf("push %d %d \n",m[exps[i]-‘A‘].a,m[exps[i]-‘A‘].b); } else if(exps[i]==‘)‘){ Matrix m2 = s.top(); s.pop(); // printf("pop %d %d \n", m2.a, m2.b); Matrix m1 = s.top(); s.pop(); // printf("pop %d %d \n", m1.a, m1.b); if(m1.b != m2.a){ok=0;break;} sum+= m1.a * m1.b * m2.b; s.push(Matrix(m1.a, m2.b)); // printf("push %d %d \n",m1.a, m2.b,); } } if(ok)printf("%d\n",sum); else printf("error\n"); } return 0; }
例题6-3 Matrix Chain Multiplication ,Uva 442
标签:
原文地址:http://www.cnblogs.com/Wade-/p/5745579.html