442 - Matrix Chain Multiplication

来源:互联网 发布:手机登录淘宝卖家中心 编辑:程序博客网 时间:2024/04/29 16:49

 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

题意:有许多个矩阵。给你一个表达式,是矩阵相乘的表达式。然后让你判断是否满足矩阵相乘的条件,如果满足,求出一共计算了多少次基本的乘法

首先要知道矩阵相乘的要求。矩阵A(M*N) 矩阵B (N*L) 即要求相乘的两矩阵的前一个列和后一个行相同,而乘法一共做了M*N*L次。

这和计算机对算术表达式计算的原理一样。用一个栈来记录操作数,用一个栈来记录操作符。而记录操作数的栈要用数组,分别记录行和列。具体见代码吧。还是比较简单的一道题

还有就是处理这类题目的时候一定要记得最后是否为空栈,以及最后清空栈。

#include<iostream>#include<cstdio>#include<cmath>#include<stack>#include<cstring>#include<string>#include<ctype.h>using namespace std;struct node{    int x;    int y;};int main (){    int n,i,j,sum=0,m[30][2];    char str[100];    node A,B;    cin>>n;    for (i=0; i<n; i++)        scanf("%s%d%d",str,&m[i][0],&m[i][1]);    while(cin>>str)    {        bool flag=true;        sum=0;        stack<node> sn;        stack<char> op;        int len=strlen(str);        for (i=0; i<len; i++)        {            if (isalpha(str[i]))            {                node temp;                temp.x=m[str[i]-65][0];                temp.y=m[str[i]-65][1];                sn.push(temp);            }            else if (str[i]=='(') op.push(str[i]);            else            {                op.pop();                B=sn.top();                sn.pop();                A=sn.top();                sn.pop();                if (A.y==B.x)                {                    sum+=A.x*A.y*B.y;                    A.y=B.y;                    sn.push(A);                }                else                {                    flag=false;                    break;                }            }            if (!flag) break;        }        if (!flag) cout<<"error"<<endl;        else cout<<sum<<endl;    }    return 0;}


	
				
		
原创粉丝点击