HDU 1082 Matrix Chain Multiplication

Posted zhaopeng938

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1082 Matrix Chain Multiplication相关的知识,希望对你有一定的参考价值。

Matrix Chain Multiplication

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2320    Accepted Submission(s): 1408


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
 
 
矩阵相乘
 1 #include<iostream>
 2 #include<string>
 3 //#include<fstream>
 4 #include<stack>
 5 using namespace std;
 6 struct pos
 7 {
 8     char ch;
 9     int row, col;
10 };
11 int main()
12 {
13     //ifstream in("data.txt");
14     int n, rows[26]={0}, cols[26]={0};
15     cin>>n;
16     while(n--)
17     {
18         char ch;
19         int row, col;
20         cin>>ch>>row>>col;
21         rows[ch-65]=row;
22         cols[ch-65]=col;
23     }
24     string s;
25     stack<pos> sta;
26     while(cin>>s)
27     {
28         int sum=0;
29         pos temp,temp1;
30         for(int i=0;i<s.size();i++)
31         {
32             if(s[i]==))
33             {
34                 temp=sta.top();sta.pop();
35                 temp1=sta.top();sta.pop();
36                 sta.pop();
37                 if(temp1.col!=temp.row)
38                 {
39                     sum=-1;
40                     break;
41                 }
42                 else
43                 {
44                     sum+=temp1.row*temp1.col*temp.col;
45                     temp.ch=*;
46                     temp.row=temp1.row;
47                     sta.push(temp);
48                 }
49             }
50             else if(s[i]==()
51             {
52                 temp.ch=(;
53                 temp.row=0;
54                 temp.col=0;
55                 sta.push(temp);
56             }
57             else
58             {
59                 temp.ch=*;
60                 temp.row=rows[s[i]-65];
61                 temp.col=cols[s[i]-65];
62                 sta.push(temp);
63             }
64         }
65         if(sum==-1)
66             cout<<"error"<<endl;
67         else
68             cout<<sum<<endl;
69     }
70     return 0;
71 }

 



















以上是关于HDU 1082 Matrix Chain Multiplication的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1082 Matrix Chain Multiplication

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

Matrix Chain Multiplication

matrix_chain_order

transition_matrix Markov chain

[Coding Made Simple] Matrix Chain Multiplication