例题6-3 Matrix Chain Multiplication ,Uva 442

这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试I don't know smile

得到的经验有以下几点:

  1. 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习
  2. 有时候代码不对,得到的结果却是对的(之后总结以下常见错误)
  3. 能用结构体,就别用数组,容易出错(暂时还不知道为什么)=>现在知道申请的数组空间在运行期间被释放,除非用malloc去申请数组
  4. 代码要规范,空格该有就要有
  5. 有些不规范表达式,不同编译器出现不同结果,注意应避免使用这类语句

像这道题主要坑在了第三点上,以后要注意避免

以下是AC代码

 第一次完成时间(大于2小时)

#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
",&n);
    for(int i=0;i<n;i++){
        char s0[10];
        char c;
        scanf("%c ",&c);
        s0[0]=c;
        scanf("%d %d
",&m[s0[0] -'A'].a, &m[s0[0] -'A'].b);
        //printf("%c %d %d
",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 
",m[exps[i]-'A'].a,m[exps[i]-'A'].b);
            }
            else if(exps[i]==')'){
                Matrix m2 = s.top(); s.pop();
            //    printf("pop %d %d 
", m2.a, m2.b);
                Matrix m1 = s.top(); s.pop();
             //   printf("pop %d %d 
", 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 
",m1.a, m2.b,);
            }
        }
        if(ok)printf("%d
",sum);
        else printf("error
");
    }
    return 0;
}

 第二次练习代码(完成时间约1小时)

 1 //UVa 442,Matrix Chain Multiplication
 2 //Example:6-3
 3 //Author:wzh
 4 //Date: 2016.8.26
 5 //Version 2
 6 
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <cctype>
10 #include <stack>
11 #include <cstdlib>
12 using namespace std;
13 #define maxn 30
14 int s[maxn][2];
15 char buf[1000];
16 int main(){
17     #ifdef D 
18     freopen("442.in","r",stdin);
19     #endif
20     int n;
21     scanf("%d ",&n);
22     for(int i=0;i<n;i++){
23         char c;int a,b;
24         scanf("%c",&c);
25         scanf("%d%d ",&s[c-'A'][0],&s[c-'A'][1]);
26         //printf("%c %d %d
",c,s[c-'A'][0],s[c-'A'][1]);
27     }
28     
29     while(fgets(buf,10000,stdin)){
30         int len=strlen(buf);
31         stack<int*> sk;
32         int sum=0;
33         int ok=1;
34         for(int i=0;i<len;i++){
35             if(buf[i]==')'){
36                 int *a,*b,*c;
37                 b=sk.top();sk.pop();
38                 //printf("pop%d %d
",b[0],b[1]);
39                 a=sk.top();sk.pop();
40                // printf("pop%d %d
",a[0],a[1]);
41                 if(a[1]==b[0]){
42                     sum+=(a[0]*a[1]*b[1]);
43                     c=(int*)malloc(sizeof(int)*2);
44                     c[0]=a[0];
45                     c[1]=b[1];
46                     sk.push(c);
47                  //   printf("push*%d %d
",c[0],c[1]);
48                 }
49                 else{
50                     ok=0;
51                     break;
52                 }
53             }
54             else if(isalpha(buf[i])){
55                 sk.push(s[buf[i]-'A']);
56                 //printf("push%d %d
",s[buf[i]-'A'][0],s[buf[i]-'A'][1]);
57             }
58         }        
59         if(ok)printf("%d
",sum);
60         else printf("error
");
61     }
62     return 0;
63 } 
原文地址:https://www.cnblogs.com/Wade-/p/5745579.html