UVa 442 (栈) Matrix Chain Multiplication

题意:

给出一个矩阵表达式,计算总的乘法次数。

分析:

基本的数学知识:一个m×n的矩阵A和n×s的矩阵B,计算AB的乘法次数为m×n×s。只有A的列数和B的行数相等时,两个矩阵才能进行乘法运算。

表达式的处理:可以用一个栈来存储,遇到字母入栈,遇到右括号将栈顶两个元素出栈,然后将乘积入栈。

 1 #include <cstdio>
 2 #include <cstring>
 3
 4 const int maxn = 30;
 5 int n;
 6 char s[100];
 7
 8 struct Matrix
 9 {
10     int n, m;
11     Matrix(int n=0, int m=0):n(n), m(m) {}
12     int times(const Matrix& rhs) const
13     {
14         if(m == rhs.n) return n * m * rhs.m;
15         return -1;
16     }
17     Matrix operator * (const Matrix& rhs) const
18     { return Matrix(n, rhs.m); }
19 }mat[maxn], stack[maxn];
20
21 int ID(char c) { return c - ‘A‘; }
22
23 int main()
24 {
25     //freopen("in", "r", stdin);
26     scanf("%d", &n);
27     getchar();
28     for(int i = 0; i < n; ++i)
29     {
30         int n, m;
31         char name;
32         scanf("%c %d %d", &name, &n, &m);
33         getchar();
34         mat[ID(name)] = Matrix(n, m);
35     }
36
37     while(scanf("%s", s) == 1)
38     {
39         int l = strlen(s);
40         int ans = 0, p = 0, ok = 1;
41         for(int i = 0; i < l; ++i)
42         {
43             if(s[i] == ‘(‘) continue;
44             else if(s[i] == ‘)‘)
45             {
46                 Matrix B = stack[--p];
47                 Matrix A = stack[--p];
48                 int t = A.times(B);
49                 if(t != -1)
50                 {
51                     ans += t;
52                     stack[p++] = A * B;
53                 }
54                 else { ok = 0; break; }
55             }
56             else
57             {
58                 stack[p++] = mat[ID(s[i])];
59             }
60         }
61
62         if(ok) printf("%d\n", ans);
63         else puts("error");
64     }
65
66     return 0;
67 }

代码君

时间: 2024-10-07 04:58:40

UVa 442 (栈) Matrix Chain Multiplication的相关文章

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

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

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

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

UVa442 Matrix Chain Multiplication(矩阵链乘)

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