uva442
来源:互联网 发布:经略幽燕我童贯 知乎 编辑:程序博客网 时间:2024/05/22 04:34
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 ( ), 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
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
#include<stdio.h> #include<string.h>int row[30],co[30];int L,r[5000],c[5000],n,top;char mat[30],a[10000],stack[10000];void f(int x,int *u,int *v){int j;for(j=0;j<=n;j++)if(stack[x]==mat[j]){*u=row[j];*v=co[j];return ;}}main(){int i,sum,ok,r1,r2,c1,c2;scanf("%d",&n);for(i=0;i<n;i++){getchar();scanf("%c%d%d",&mat[i],&row[i],&co[i]);}getchar();while(gets(a)){memset(r,0,sizeof(r));memset(c,0,sizeof(c));ok=sum=top=0;L=strlen(a);if(a[0]!='(') {printf("0\n");continue;}for(i=0;i<L;i++){if(a[i]!=')')stack[++top]=a[i];else if(a[i]==')'){if(r[top-1]==0&&c[top-1]==0)f(top-1,&r[top-1],&c[top-1]);//获得行列数if(r[top]==0&&c[top]==0)f(top,&r[top],&c[top]);//获得行列数if(c[top-1]==r[top])sum+=r[top-1]*r[top]*c[top];else if(c[top-1]!=r[top]){printf("error\n");ok=1;break;}while(stack[top]!='(')top--;stack[top]='@';r[top]=r[top+1];c[top]=c[top+2];r[top+1]=c[top+1]=r[top+2]=c[top+2]=0;//注意回零}}if(!ok)printf("%d\n",sum);}return 0;}
- UVA442
- uva442
- UVA442
- UVa442
- uva442
- uva442
- uva442
- UVa442
- UVa442
- UVa442
- UVa442
- uva442 栈
- UVA442栈
- uva442 Matrix Chain Multiplication
- uva442 Matrix Chain Multiplication
- uva442-矩阵链乘
- uva442-Matrix Chain Multiplication
- UVA442 Matrix Chain Multiplication
- 网络风暴
- ios 摇一摇功能的实现
- 汇编 [计算平台长度] 用的自己的想法,至于长度的显示,超过双数,修改一下就行了,懒得修改了!
- NYOJ 6 喷水装置(一)
- [shell] 一些好玩或者可能有用的shell命令
- uva442
- ubuntu下启用FrameBuffer模块
- 关于WPF自定义的求助贴
- HDU3555:Bomb(数位DP)
- cgdb安装与使用
- tomcat做成windows服务
- Windows下Qt应用程序可以连接本地MYSQL不能连接linux的问题
- linux c++找不到dll
- Redis数据库