HDU 1082 matrix chain Multiplication

Source: Internet
Author: User
Matrix chain Multiplication

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 797 accepted submission (s): 553

Problem descriptionmatrix multiplication problem is a typical example of dynamical programming.

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 saved med 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.

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 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 ")"
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 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

000error%error350015000405004750015125
# Include <iostream> # include <cstdio> # include <stack> # include <string> using namespace STD; struct tnode {int R, L;} node [30]; // records the rows and columns corresponding to the matrix name given by the record. Since the names are from 'A' to 'Z', it is best to convert the subscript into a letter corresponding number, that is, 'A 'to 1' for 2... char s [1005]; int row [1005], line [1005]; // The row and column of the corresponding matrix are recorded in the simulation stack, where int ans, t, n; bool CAL () {int I, j, R1, L1, R2, L2; If (t = 1) return true; R1 = row [T]; L1 = line [T]; r2 = row [T-1]; L2 = line [T-1]; If (L1! = R2) return false; else {ans + = R1 * L2 * L1; t --; row [T] = R1; line [T] = L2; return true ;}} int main () {int I, j, T1, T2, flag; char C; scanf ("% d", & N); for (I = 1; I <= N; I ++) {getchar (); scanf ("% C % d", & C, & T1, & T2 ); node [C-'A' + 1]. L = T1; node [C-'A' + 1]. R = t2;} getchar (); While (gets (s )! = NULL) {ans = 0; t = 0; flag = 0; for (I = 0; s [I]! = '\ 0'; I ++) {If (s [I] =' (') continue; else if (s [I]> = 'A' & S [I] <= 'Z ') // if it is a letter, go to the stack {row [++ T] = node [s [I]-'A' + 1]. r; line [T] = node [s [I]-'A' + 1]. l;} else if (s [I] = ') // if it is') ', two computations are generated and then added to the stack {If (! Cal () {flag = 1; printf ("error \ n"); break ;}} if (! Flag) printf ("% d \ n", ANS);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.