杭电acm—1082 Matrix Chain Multiplication

来源:互联网 发布:linux服务器书籍 编辑:程序博客网 时间:2024/06/08 13:42

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082

Matrix Chain Multiplication

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


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
9A 50 10B 10 20C 20 5D 30 35E 35 15F 15 5G 5 10H 10 20I 20 25ABC(AA)(AB)(AC)(A(BC))((AB)C)(((((DE)F)G)H)I)(D(E(F(G(HI)))))((D(EF))((GH)I))
 

Sample Output
000error10000error350015000405004750015125
 

Source
University of Ulm Local Contest 1996

这个题目,弄懂矩阵相乘就行了,不了解的朋友,可以百度一下,说白了,这个题目就是抓住三点,假设A:3行2列,B:2列三行,两个矩阵,第一点:A*B,一共乘的次数为:A的行乘以B的行列,即:3*2*3,第二点:能相乘的条件是,A的列等于B的行,第三点:假设A*B=C,那么C的行,为A的行,列,为B的列。自己在本子上面推算一下吧。
AC代码如下:(如有错误和疑问,请各位不吝指出)
#include<stdio.h>#include<string.h>int hgs[30][2];int s[1000][2];int main(){int n;char str[1000]; scanf("%d",&n);for(int i=0;i<n;i++){char c;int x,y;getchar();scanf("%c%d%d",&c,&x,&y);hgs[c-'A'][0]=x;hgs[c-'A'][1]=y;}getchar();while(scanf("%s",str)!=EOF){int length=strlen(str);int sum=0,k=0,flag=0;if(length==1)  printf("0\n");else{for(int i=0;i<length;i++){if(str[i]=='(')  continue;if(str[i]==')'){//一旦出现')',意味着有两个要相乘 if(s[k-2][1]!=s[k-1][0]){//判断是否可以相乘 flag=1;break;}sum+=s[k-2][0]*s[k-1][0]*s[k-1][1];//A的行乘以B的行和列 s[k-2][1]=s[k-1][1];//注意把B的列赋给A的列 k--;//A乘B得到A,所以k-- }else{s[k][0]=hgs[str[i]-'A'][0];s[k][1]=hgs[str[i]-'A'][1];k++;}}if(flag)  printf("error\n");else  printf("%d\n",sum);}}return 0;}


原创粉丝点击