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




大意就是不断的合并矩阵然后计算代价 ,栈的模拟


  1 #include <cstdio>
  2 #include <string>
  3 #include <iostream>
  4 #include <stack>
  5 #include <string.h>
  6 #include <cctype>
  7 using namespace std;
  8 const int maxn=5000;
  9 int n;
 10 int row[maxn],matrix[maxn];   //保存每个矩阵的行和列的个数
 11 int main()
 12 {
 13         scanf("%d\n",&n);
 14         char c[10];
 15         int now;
 16         for(int i=0;i<n;i++)
 17         {
 18             scanf("%s",&c[0]);
 19             now=c[0]-'A';
 20             scanf("%d %d",&row[now],&matrix[now]);
 21             //cout<<c<<" "<<row[now]<<" "<<matrix[now]<<endl;
 22         }
 23         stack<int> r;
 24         stack<int> m;
 25         int ans=0;
 26         int error=0;
 27         char s[maxn];
 28         int ps=0;
 29         while(scanf("%s",&s[0])!=EOF)
 30         {
 31 
 32             while(ps<strlen(s))
 33             {
 34 
 35                 if(s[ps]=='(')
 36                 {
 37                     r.push(-1);
 38                     m.push(-1);
 39                 }
 40                 else if(isalpha(s[ps]))
 41                 {
 42                     if(r.empty() || r.top()==-1)
 43                     {
 44                         r.push(row[s[ps]-'A']);
 45 
 46                         m.push(matrix[s[ps]-'A']);
 47                     }
 48                    else  if(r.top()>0)
 49                     {
 50                         int tr,tm;
 51                         tr = row[s[ps]-'A'];
 52 
 53                         tm = matrix[s[ps]-'A'];
 54 
 55                         if(m.top() != tr)
 56                         {
 57                             printf("error\n");
 58                             error=1;
 59                             break;
 60                         }
 61                         else
 62                         {
 63                             m.pop();
 64                             m.push(tm);
 65                             ans+=r.top()*tr*m.top();
 66                         }
 67                     }
 68                 }
 69                 else if(s[ps]==')')
 70                 {
 71                     int tx,ty;
 72                     tx=r.top();
 73                     ty=m.top();
 74                     r.pop();
 75                     m.pop();
 76                     r.pop();
 77                     m.pop();
 78                     if(!r.empty())
 79                     if(r.top()>0)
 80                     {
 81                         if(tx!=m.top())
 82                         {
 83                             printf("error\n");
 84                             error=1;
 85                             break;
 86                         }
 87                         else
 88                         {
 89                             m.pop();
 90                             m.push(ty);
 91                             ans+=r.top()*tx*ty;
 92                         }
 93                     }
 94                     else
 95                     {
 96                         r.push(tx);
 97                         m.push(ty);
 98                     }
 99                 }
100                 ps++;
101             }
102             if(!error)
103                 printf("%d\n",ans);
104             while(!r.empty())
105             {
106                 r.pop();
107                 m.pop();
108             }
109             error=0;
110             ans=0;
111             ps=0;
112         }
113     return 0;
114 }

 

posted @ 2013-10-15 12:29  doubleshik  阅读(192)  评论(0编辑  收藏  举报