UVa442 Matrix Chain Multiplication (栈)

链接:http://acm.hust.edu.cn/vjudge/problem/19085分析:用栈来实现一个简单的表达式求值。输入表达式保证合法,比如(A(BC))把BC看成一个整体D那么(A(BC))就是AD,所以这些表达式都是在不断重复嵌套相同的结构A*B。遍历表达式,遇到字母则将对应矩阵压入栈中,遇到右括号则将栈顶两个矩阵弹出,判断是否能进行乘法,如果能则累加乘法次数,再将相乘后的矩阵压栈,如果不能则将error置为true,退出循环。
 1 #include <iostream>
 2 #include <string>
 3 #include <stack>
 4 using namespace std;
 5
 6 struct Matrix {
 7     int a, b;
 8     Matrix(int a = 0, int b = 0): a(a), b(b) {}
 9 } m[26];
10
11 stack<Matrix> s;
12
13 int main() {
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) cout << "error" << endl; else cout << ans << endl;
38     }
39     return 0;
40 }
时间: 2024-08-05 23:40:34

UVa442 Matrix Chain Multiplication (栈)的相关文章

UVa442 Matrix Chain Multiplication(矩阵链乘)

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

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

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

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

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

[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

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

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 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