zoj 1094 Matrix Chain Multiplication

Matrix Chain Multiplication

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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 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 (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 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 <iostream>
  2 #include <cmath>
  3 #include <cstdio>
  4 #include <vector>
  5 #include <list>
  6 #include <string>
  7 #include <cstring>
  8 #include <cstdio>
  9 #include <algorithm>
 10 #include <set>
 11 #include <stack>
 12 
 13 using namespace std;
 14 
 15 int count(const pair<int, int>& p1, const pair<int, int>& p2)
 16 {
 17     if (p1.second != p2.first)
 18         return -1;
 19     return p1.first * p1.second * p2.second;
 20 }
 21 
 22 int main()
 23 {
 24     int n;
 25     cin >> n;
 26     pair<int, int> matrix[30];
 27     while (n--)
 28     {
 29         char ch;
 30         cin >> ch;
 31         int m, n;
 32         cin >> m >> n;
 33         matrix[ch - 'A'] = pair<int, int>(m, n);
 34     }
 35     
 36     string exp;
 37     while (cin >> exp)
 38     {
 39         stack< pair<int, int> > sta;
 40         int brackets = 0, cnt = 0, err = 0;
 41         for (int i = 0; i < exp.size(); ++i)
 42         {
 43             /*    if (exp[i] == '(')
 44                     sta.push('(');
 45                 else if (isupper(exp[i]))
 46                 {
 47                     if (sta.empty() || sta.top() == '(')
 48                         sta.push(exp[i]);
 49                     else
 50                     {
 51                         char ch = sta.top();
 52                         sta.pop();
 53                         pair<int, int> op1 = matrix[ch - 'A'], op2 = matrix[exp[i] - 'A'];
 54                         int c = count(op1, op2);
 55                         if (c == -1)
 56                         {
 57                             cout << "error" << endl; 
 58                             break;
 59                         }
 60                         else
 61                         {
 62                             matrix[26] = pair<int, int>(op1.first, op2.second);
 63                         }
 64                     }
 65                 }*/
 66             if (exp[i] == '(')
 67                 ++brackets;
 68             else if (isupper(exp[i]))
 69             {
 70                 if (sta.empty() || brackets > 0)
 71                     sta.push(matrix[exp[i] - 'A']);
 72                 else
 73                 {
 74                     pair<int, int> op1, op2;
 75                     op1 = sta.top();
 76                     sta.pop();
 77                     op2 = matrix[exp[i] - 'A'];
 78                     int c = count(op1, op2);
 79                     if (c == -1)
 80                     {
 81                         cout << "error" << endl;
 82                         err = 1;
 83                         break;
 84                     }
 85                     else
 86                         cnt += c;
 87                     sta.push(pair<int, int>(op1.first, op2.second));
 88                     
 89                 }
 90             }
 91             else if (exp[i] == ')')
 92             {
 93                 pair<int, int> op1, op2;
 94                 op1 = sta.top();
 95                 sta.pop();
 96                 op2 = sta.top();
 97                 sta.pop();
 98                 int c = count(op2, op1);
 99                 if (c == -1)
100                 {
101                     cout << "error" << endl;
102                     err = 1;
103                     break;
104                 }
105                 else
106                     cnt += c;
107                 sta.push(pair<int, int>(op2.first, op1.second));
108                 brackets--;
109                 
110             }
111         }    //end-for
112         if (!err)
113             cout << cnt << endl;
114         
115     }
116 }

 


Source: University of Ulm Local Contest 1996

 

转载于:https://www.cnblogs.com/jackwang822/p/5360637.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值