UVA 442(栈的应用)

来源:互联网 发布:姚明nba打球平均数据 编辑:程序博客网 时间:2024/05/17 03:06

F - Matrix Chain Multiplication
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status
Appoint description: 

Description

Download as PDF


 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

题意给出个n个矩阵的行和列,按照括号优先级的顺序进行矩阵乘法,计算总的乘法次数,不能相乘的输出错误。

遇到字母直接入栈,遇到右括号就计算。


#include<stdio.h>#include<stack>#include<algorithm>#include<cctype>#include<string.h>int m[30],n[30];char s[1005],c;int main(){    using std::stack;    int t;    //freopen("in.txt","r",stdin);    scanf("%d",&t);    for(int i=1; i<=t; i++)    {        scanf("%s",&c);        scanf("%d%d",&m[c-'A'],&n[c-'A']);    }    getchar();    while(gets(s)!=NULL)    {        stack<int>M,N;        int len=strlen(s),ans=0,lose=0;        for(int i=0; i<len; i++)        {            if(s[i]==')')            {                int u=M.top(),v=N.top();                M.pop();                N.pop();                if(!M.empty())                {                    if(N.top()!=u)                    {                        lose=1;                        break;                    }                    else                    {                        ans+=M.top()*N.top()*v;                        N.pop();                        N.push(v);                    }                }            }            if(isalpha(s[i]))            {                M.push(m[s[i]-'A']);                N.push(n[s[i]-'A']);            }        }        int am,bm=-1,an,bn=-1;//判断不含括号的情况        if(!M.empty())        {            am=M.top();            M.pop();            an=N.top();            N.pop();        }        if(!M.empty())        {            bm=M.top();            bn=N.top();        }        if(bm!=-1)        {            if(an!=bm)lose=1;            else ans+=am*an*bn;        }        if(!lose)printf("%d\n",ans);        else puts("error");    }    return 0;}


0 0
原创粉丝点击