例题6-3 Matrix Chain Multiplication ,Uva 442

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

得到的经验有以下几点:

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

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

以下是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;
}
时间: 2024-10-22 03:57:56

例题6-3 Matrix Chain Multiplication ,Uva 442的相关文章

Matrix Chain Multiplication UVA 442 (栈 表达式求值)

说说: 其实这道题是栈这个数据结构最经典的运用,即表达式的求值.与一般情况不同的是,此次要求的运算数是矩阵.在整个解析表达式的过程中,无非遇到三类字符,一个是'(',另一个是')',剩下的就是运算数了.首先,在遇到'('的时候,栈指针自动加一,并将栈顶元素的行数和列数都设置为-1,这样就不会和正常的运算数混淆了.如果遇到的是运算数,首先要判断当前的栈顶元素是否为运算数(当然,还要注意栈为空的特殊情况).若是,则直接将新的运算数与栈顶运算数进行计算,否则将新运算数入栈.还有最后一种情况就是遇到')

[2016-02-05][UVA][442][Matrix Chain Multiplication]

[2016-02-05][UVA][442][Matrix Chain Multiplication] UVA - 442 Matrix Chain Multiplication Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Suppose you have to evaluate an expression like A*B*C*D*E where

UVA 442 二十 Matrix Chain Multiplication

Matrix Chain Multiplication Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 442 Appoint description:  System Crawler  (2015-08-25) Description Suppose you have to evaluate an expression like A*B*C*D*E

stack UVA 442 Matrix Chain Multiplication

题目传送门 /* stack 容器的应用:矩阵的表达式求值 A 矩阵是a * b,B 矩阵是b * c,则A * B 是a * c */ #include <cstdio> #include <iostream> #include <algorithm> #include <stack> #include <cmath> #include <cstring> #include <string> using namespac

UVa 442 Matrix Chain Multiplication(矩阵链乘,模拟栈)

题意  计算给定矩阵链乘表达式需要计算的次数  当前一个矩阵的列数等于后一个矩阵的行数时  他们才可以相乘  不合法输出error 输入是严格合法的  即使只有两个相乘也会用括号括起来  而且括号里最多有两个 那么就很简单了 遇到字母直接入栈  遇到反括号计算后入栈  然后就得到结果了 #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int N = 1000;

UVA Matrix Chain Multiplication

题目如下: Matrix Chain Multiplication Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are performed is arbitrary. However, the numb

442 - Matrix Chain Multiplication

Matrix Chain Multiplication Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are performed is arbitrary. However, the number of

UVa442 Matrix Chain Multiplication(矩阵链乘)

UVa442 Matrix Chain Multiplication(矩阵链乘) 题目链接:Uva442 题目描述:输入n个矩阵的维度和一个矩阵链乘的表达式,输出乘法的次数,如果乘法无法进行,则输出error. 题目分析: 栈对表达式求值有着特殊的作用,本题表达式简单,可以用一个栈来完成,遇到字母时入栈,遇到右括号时出栈并且计算,之后算出的结果入栈. <<<<<<<<<<<<<<<<<<<&l

Matrix Chain Multiplication(表达式求值用栈操作)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1382    Accepted Submission(s): 905 Problem Description Matrix mul

ACM学习历程——UVA442 Matrix Chain Multiplication(栈)

Description Matrix Chain Multiplication  Matrix Chain Multiplication  Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are perfo