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 multiplication problem is a typical example of dynamical programming. 

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 elementary multiplications needed strongly depends on the evaluation order you choose.
For example, let A be a 50*10 matrix, B a 10*20 matrix and C a 20*5 matrix.
There are two different strategies to compute A*B*C, namely (A*B)*C and A*(B*C).
The first one takes 15000 elementary multiplications, but the second one only 3500. 

Your job is to write a program that determines the number of elementary multiplications needed for a given evaluation strategy. 
 
Input
Input consists of two parts: a list of matrices and a list of expressions.
The first line of the input file contains one integer n (1 <= n <= 26), representing the number of matrices in the first part. The next n lines each contain one capital letter, specifying the name of the matrix, and two integers, specifying the number of rows and columns of the matrix. 
The second part of the input file strictly adheres to the following syntax (given in EBNF): 

SecondPart = Line { Line } <EOF>
Line = Expression <CR>
Expression = Matrix | "(" Expression Expression ")"
Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"
 
Output
For each expression found in the second part of the input file, print one line containing the word "error" if evaluation of the expression leads to an error due to non-matching matrices. Otherwise print one line containing the number of elementary multiplications needed to evaluate the expression in the way specified by the parentheses. 
 
Sample Input
9 A 50 10 B 10 20 C 20 5 D 30 35 E 35 15 F 15 5 G 5 10 H 10 20 I 20 25 A B C (AA) (AB) (AC) (A(BC)) ((AB)C) (((((DE)F)G)H)I) (D(E(F(G(HI))))) ((D(EF))((GH)I))
 
Sample Output
0 0 0 error 10000 error 3500 15000 40500 47500 15125
 
Source
 
题意:这里介绍一种写结构体重构造函数比较神奇的写法;详见刘汝佳大神的代码
题解:一道水题,注意表达式求值的题应当马上想到用栈来实现
自己的ac代码
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<string>
 4 #include<algorithm>
 5 #include<map>
 6 #include<iostream>
 7 using namespace std;
 8 #define N 30
 9 #define M 50000
10 struct Mnode{
11     int m;
12     int n;
13 }mm[N];
14 Mnode stk[M];
15 
16 int main()
17 {
18     int t;
19     while(~scanf("%d",&t))
20     {
21         map<char,int> mp;
22         for(int i = 0; i <  t; i++)
23         {
24             char ch;
25             int m, n;
26             getchar();
27             scanf("%c %d %d",&ch,&m,&n);
28             mp[ch] = i;
29             mm[i].m = m;
30             mm[i].n = n;
31         }
32         char ml[1000];
33         while(~scanf("%s",ml))
34         {
35         int top = 0;
36         int ans = 0;
37         bool flag = 1;
38         for(int i = 0; i < strlen(ml); i++)
39         {
40             if(ml[i]<='Z'&&ml[i]>='A'){
41                 Mnode tm;
42                 tm.m = mm[mp[ml[i]]].m;
43                 tm.n = mm[mp[ml[i]]].n;
44                 stk[top++] = tm;
45             }
46             else if(ml[i]==')'){
47                 Mnode tm1,tm2,tm3;
48                 tm2 = stk[--top];
49                 tm1 = stk[--top];
50                 if(tm1.n!=tm2.m){ puts("error");    flag = 0; break;    }
51                 tm3.m = tm1.m;
52                 tm3.n = tm2.n;
53                 ans+=tm1.m*tm1.n*tm2.n;
54                 stk[top++] = tm3;
55             }
56         }
57         if(flag) printf("%d
",ans);
58         }
59     }
60     return 0;
61 }

刘汝佳大神的代码,注意其中的构造函数的写法,表示如果没有参数的时候自动默认两个参数值都是0

 1 #include<cstdio>
 2 #include<stack>
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6 struct Matrix{
 7     int a, b;
 8     Matrix(int a = 0, int b = 0):a(a),b(b){}
 9 }m[26];
10 stack<Matrix> s;
11 
12 int main()
13 {
14     int n;
15     cin>>n;
16     for(int i = 0; i < n; i++){
17         string name;
18         cin>>name;
19         int k = name[0]-'A';
20         cin>>m[k].a>>m[k].b;
21     }
22     string expr;
23     while(cin>>expr){
24         int len = expr.length();
25         bool error = false;
26         int ans = 0;
27         for(int i = 0; i < len; i++){
28             if(isalpha(expr[i])) s.push(m[expr[i]-'A']);
29             else if(expr[i]==')'){
30                 Matrix m2 = s.top();s.pop();
31                 Matrix m1 = s.top();s.pop();
32                 if(m1.b!=m2.a){error = true; break;}
33                 ans += m1.a*m1.b*m2.b;
34                 s.push(Matrix(m1.a,m2.b));
35             }
36         }
37         if(error) printf("error
"); else printf("%d
",ans);
38     }
39     return 0;
40 }
 
原文地址:https://www.cnblogs.com/shanyr/p/5200606.html