码迷,mamicode.com
首页 > 其他好文 > 详细

[2016-02-05][UVA][442][Matrix Chain Multiplication]

时间:2016-02-05 11:29:51      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

[2016-02-05][UVA][442][Matrix Chain Multiplication]
UVA - 442
Time Limit: 3000MSMemory Limit: Unknown64bit IO Format: %lld & %llu

 Status

Description

技术分享

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 ( 技术分享 ), 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

  • 时间:2016-02-05 01:42:28 星期五
  • 题目编号:UVA 442
  • 题目大意:给定若干个矩阵,给出一个乘法表达式,求表达式的次数
  • 方法:用栈模拟整个过程,遇到字母的时候入栈,遇到括号的时候出栈,
  • 然后把运算结果整合成一个新的矩阵入栈,直到结束
  • 解题过程遇到问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
#define CLR(x,y) memset((x),(y),sizeof((x)))
#define getint(x) int (x);scanf("%d",&(x))
#define get2int(x,y) int (x),(y);scanf("%d%d",&(x),&(y))
#define get3int(x,y,z) int (x),(y),(z);scanf("%d%d%d",&(x),&(y),&(z))
#define getll(x) LL (x);scanf("%I64d",&(x))
#define get2ll(x,y) LL (x),(y);scanf("%I64d%I64d",&(x),&(y))
#define get3ll(x,y,z) LL (x),(y),(z);scanf("%I64d%I64d%I64d",&(x),&(y),&(z))
#define getdb(x) double (x);scanf("%lf",&(x))
#define get2db(x,y) double (x),(y);scanf("%lf%lf",&(x),&(y))
#define get3db(x,y,z) double (x),(y),(z);scanf("%lf%lf%lf",&(x),&(y),&(z))
 
#define getint2(x) scanf("%d",&(x))
#define get2int2(x,y) scanf("%d%d",&(x),&(y))
#define get3int2(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define getll2(x) scanf("%I64d",&(x))
#define get2ll2(x,y) scanf("%I64d%I64d",&(x),&(y))
#define get3ll2(x,y,z) scanf("%I64d%I64d%I64d",&(x),&(y),&(z))
#define getdb2(x) scanf("%lf",&(x))
#define get2db2(x,y) scanf("%lf%lf",&(x),&(y))
#define get3db2(x,y,z) scanf("%lf%lf%lf",&(x),&(y),&(z))
 
#define getstr(str) scanf("%s",str)
#define get2str(str1,str2) scanf("%s%s",str1,str2)
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--)
#define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++)
#define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--)
const int maxn = 10000 + 100;
struct matrix{
        int m,n;
        matrix(int a = 0,int b = 0):m(a),n(b){}
}a[26];
char op[maxn];
int main()
{
        getint(n);
        FOR(i,0,n){
                char b[10];
                getstr(b);
                get2int(m,n);
                a[b[0] - ‘A‘].m = m;
                a[b[0] - ‘A‘].n = n;
        }
        int res = 0;
        while(~scanf("%s",op)){
                stack<matrix> s;
                char *p = op;
                int res = 0;
                int err = 0;
                while(*p && !err){
                        if(isupper(*p)){
                                s.push(a[*p-‘A‘]);
                        }else if(*p == ‘)‘){
                                matrix a,b;
                                b = s.top();s.pop();
                                a = s.top();s.pop();
                                if(a.n != b.m)
                                        err = 1;
                                else {
                                        res += a.m*a.n*b.n;
                                        s.push(matrix(a.m,b.n));
                                }
                        }
                        p++;
                }
                if(err)
                        puts("error");
                else printf("%d\n",res);
        }
    return 0;
}





[2016-02-05][UVA][442][Matrix Chain Multiplication]

标签:

原文地址:http://www.cnblogs.com/qhy285571052/p/5182634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!