UVA 442二十 Matrix Chain Multiplication

來源:互聯網
上載者:User

標籤:

Matrix Chain MultiplicationTime Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %lluSubmit Status Practice UVA 442Appoint description: System Crawler  (2015-08-25)

Description

 

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
 1 #include <stdio.h> 2 #include <string.h> 3 #include <stack> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7  8 struct Node 9 {10     int a;11     int b;12 };13 14 stack <Node> s;15 16 int main()17 {18     Node m[26];19     int n;20     int i,j,k,flg;21     char nu[10],str[1000];22     while(scanf("%d",&n)!=EOF)23     {24         for(i=1;i<=n;i++)25         {26             scanf("%s",nu);27             k=nu[0]-‘A‘;28             scanf("%d %d",&m[k].a,&m[k].b);29         }30 31         while(scanf("%s",str)!=EOF)32         {33             if(!s.empty())34                 s.pop();35             int l=strlen(str),final=0;;36             flg=0;37             for(i=0;i<l;i++)38             {39                 if(isalpha(str[i]))40                     s.push(m[str[i]-‘A‘]);41                 else if(str[i]==‘)‘)42                 {43                     Node m1,m2,mm;44                     m2=s.top();s.pop();45                     m1=s.top();s.pop();46                     if(m1.b!=m2.a)47                     {48                         flg=1;49                         break;50                     }51                     final=final+m1.b*m1.a*m2.b;52                     mm.a=m1.a,mm.b=m2.b;53                     s.push(mm);54                 }55             }56             if(flg==1)57                 printf("error\n");58             else59                 printf("%d\n",final);60         }61     }62     return 0;63 }
View Code

 

UVA 442二十 Matrix Chain Multiplication

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.