第六章 - Matrix Chain Multiplication - uva442 - stack

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 orderyou choose.For example, let A be a 50*10 matrix, B a 10*20 matrix and C a 20*5 matrix. There are twodifferent 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 neededfor a given evaluation strategy.InputInput 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 ofmatrices in the first part. The next n lines each contain one capital letter, specifying the name of thematrix, 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 } Line = Expression Expression = Matrix | "(" Expression Expression ")"Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"OutputFor 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 oneline containing the number of elementary multiplications needed to evaluate the expression in the wayspecified by the parentheses.Sample Input9A 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 Output000error10000error350015000405004750015125

分析:

此题的思路是读入时,遇到字母,入栈,遇到')'计算。

此题有几个tips需要注意:

1)要存数组的序号(A,B,C……)吗?显然是不需要的,只需要入读字符让该数组的信息存到对应的位置就好了(比如A存到0,B存到1),这也方便计算的时候找到对应的数组(比如说计算时(AB)只需要找到M[0],与M[1]相乘就行,不用再遍历数组找数组序号为'A','B')

2)入栈时,如的是什么?当然是把整个矩阵入栈了,这样方便我们计算,连续出两次栈,结算得到的新矩阵入栈。

代码如下:

#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
#include<string>
using namespace std;
struct A{
    int x,y;
}M[27];
stack<A> num;
int n;
int main(){
    char ch[3];
    int k;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%s",&ch);
        k=ch[0]-'A';
        scanf("%d%d",&M[k].x,&M[k].y);
    }
    string s;

    while(cin>>s){
        int ans=0;
        int flag=1,len=s.length();
        if(len==1){printf("0\n");flag=0;continue;}
        for(int i=0;i<len;i++){
            if(s[i]=='(')continue;
            if(isalpha(s[i]))num.push(M[s[i]-'A']);
            if(s[i]==')'){
                A t1=num.top();num.pop();
                A t2=num.top();num.pop();
                if(t2.y!=t1.x){printf("error\n");flag=0;break;}
                ans+=t2.x*t2.y*t1.y;
                A t3;
                t3.x=t2.x; t3.y=t1.y;
                num.push(t3);
            }
        }
        if(flag)printf("%d\n",ans);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值