Matrix Chain Multiplication

Uva 442 – Matrix Chain Multiplication

Matrix Chain Multiplication

Time limit: 3.000 seconds

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 integern(), representing the number of matrices in the first part. The nextnlines 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

// Address:Uva 442 Matrix Chain Multiplication (矩阵链相乘)// level: mess // Classify:Data Structure (stack) // Verdict: Accepted [one times] // UVa Run Time: 0.004s // Submission Date: 2012-12-04 12:09:44 // My ID: xueying // Usetime:2 hours// [解题方法] // 矩阵相乘的条件, 若一矩阵的列数与另一矩阵的行数相等,则可定义这两个矩阵的乘积。// 如 A 是 m×n 矩阵和 B 是 n×p矩阵,服务器空间,它们是乘积 AB 是一个 m×p 矩阵// (AB)[i, j] = A[i, 1] * B[1, j] + A[i, 2] * B[2, j] + … + A[i, n] * B[n, j] // 对于(AB)[i, j],网站空间,i由矩阵A提供,香港服务器,而j由矩阵B提供。// 对于A[i,j](1<= i <= m, 1<= j <= n), B[i,j](1<= i <= n, 1<= j <= p), *运算的次数为:m*n*q#include<stdio.h>#include<string.h>#include<ctype.h>temp[1000];//letter的结构体 typedef struct{char lter;int row, column;}Matrix;Matrix stack[1000];int main(){int i, j, len, n, t, m, num, flag, rear, front, k;int row, column;Matrix Mtx[27];char letter;for(i=0; i<27; ++i)Mtx[i].row = Mtx[i].column = 0;scanf(, &n);(t=1; t<=n; ++t){getchar();scanf(, &letter);m = letter – 65;Mtx[m].lter = letter;scanf(, &Mtx[m].row);scanf(, &Mtx[m].column);}memset(temp, , sizeof(temp));(scanf(, temp) != EOF){for(i=0; i<1000; ++i)stack[i].lter = , stack[i].row = stack[i].column = 0;num = flag = 0;rear = front = 1;len = strlen(temp);//进栈各种情况判断(i=0; i<len; ++i){if(isalpha(temp[i])){m = temp[i] – 65;){stack[0].lter = temp[i];stack[0].row = Mtx[m].row, stack[0].column = Mtx[m].column;}else{if(stack[0].column != Mtx[m].row){flag = 1;break;}else{num += stack[0].row*stack[0].column*Mtx[m].column;stack[0].column = Mtx[m].column;}}}){){stack[rear++].lter = temp[i];}else{stack[rear++] = stack[0];stack[rear++].lter = temp[i];stack[;}}else{rear–;if(rear != front){rear–;){if(stack[rear].column != stack[0].row){flag = 1;break;}else{num += stack[rear].row*stack[rear].column*stack[0].column;stack[0].lter = stack[rear].lter;stack[0].row = stack[rear].row;}}else rear++;}}});, num);}return 0;}//Learn from it: //定义的结构体变量可以用等号相互赋值//字符变量实质上是一个字节的整型变量,可以把0~127之间的整数赋给一个字符变量

posted on

青春不是年华,而是心境;青春不是桃面丹唇柔膝,

Matrix Chain Multiplication

相关文章:

你感兴趣的文章:

标签云: