uva442

来源:互联网 发布:经略幽燕我童贯 知乎 编辑:程序博客网 时间:2024/05/22 04:34


 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

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;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 腌牛肉太咸了怎么办 八宝粥的拉环断了怎么办 八宝粥易拉罐拉环断了怎么办 吃完辣的胃难受怎么办 天天呆在家很烦怎么办 吃多了荔枝上火怎么办 猛犸牙牌子裂了怎么办 玩游戏电脑卡了怎么办 电脑打开卡在选项界面怎么办? 红警突然卡死怎么办 柯基不吃狗粮怎么办 貔貅嘴巴磕破了怎么办 开光貔貅牙磕了怎么办 玉貔貅鼻子碎了怎么办 开光的貔貅摔坏了怎么办 貔貅摔坏了耳朵怎么办? 貔貅摔坏了一点点怎么办 天猫删评价扣4分怎么办 暴风影音下载电视剧下载不了怎么办 fm2017引援电脑买了怎么办 退休时医保不够二十年怎么办 和的面迟迟不发怎么办 做面条的面发了怎么办 发不起来的面团怎么办 如果婚后买房离婚后财产怎么办 空腹体检前喝水了怎么办 莆田市风俗婴儿死了怎么办后事 上司给你换不好岗位怎么办 领导要把我调走怎么办 美国上庭遇法官退休案子怎么办 夫妻一套房离婚户囗怎么办 正营生不了副团怎么办 浅色衣服被染色了怎么办 解大便解不出来怎么办 强拆挖掘机被村民扣押怎么办 小孩哭的时候接不上气怎么办 昆明市长信箱发不出去怎么办 中国公民在香港想去澳门怎么办 面包车不给贴膜怎么办 在香港海关被扣怎么办 发现被医院骗了怎么办