442 - Matrix Chain Multiplication
2016-05-08 19:53
453 查看
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 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 Specification
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 ( tex2html_wrap_inline28 ), 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 Specification
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
输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数。如果乘法无法进行,输出error。假定A是m*n矩阵,B是n*p矩阵,那么AB是m*p矩阵,乘法次数为m*n*p。如果A的列数不等于B的行数,则乘法无法进行。
例如,A是50*10的,B是10*20的,C是20*5的,则(A(BC))的乘法次数为10*20*5(BC的乘法次数)+ 50*10*5((A(BC))的乘法次数)= 3500。
#include <cstdio> #include <stack> #include <string> #include <iostream> using namespace std; // 矩阵结构 struct Matrix{ int first; int second; }; // 最多26个矩阵 Matrix m[26]; // 矩阵栈 stack<Matrix> st; // 矩阵链乘 int matrixMul(string exp) { // 只有1个矩阵 if(exp.length() == 1) { return 0; } else { int ans = 0; // 简单的表达式解析 for(int i = 0; i < exp.length(); i++) { // A-Z进栈 if(exp[i] >= 'A' && exp[i] <= 'Z') { st.push(m[exp[i] - 'A']); } else if(exp[i] == ')') { // ),出栈两个矩阵,算出乘积次数并将结果矩阵入栈 Matrix m2 = st.top(); st.pop(); Matrix m1 = st.top(); st.pop(); // 不能做乘法,返回-1 if(m1.second != m2.first) { return -1; } // 结果累加 ans += m1.first * m1.second * m2.second; Matrix m3; m3.first = m1.first; m3.second = m2.second; // 结果矩阵入栈 st.push(m3); } } return ans; } } int main() { int n; scanf("%d", &n); // 矩阵输入 for(int i = 0; i < n; i++) { string s; int first; int second; cin >> s >> first >> second; m[s[0] - 'A'].first = first; m[s[0] - 'A'].second = second; } string exp; while(cin >> exp) { int ans = matrixMul(exp); if(ans < 0) { printf("error\n"); } else { printf("%d\n", ans); } } return 0; }
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge