您的位置:首页 > 大数据 > 人工智能

Uva 442 - Matrix Chain Multiplication

2012-12-04 22:06 555 查看
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 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

9
A 50 10
B 10 20
C 20 5
D 30 35
E 35 15
F 15 5
G 5 10
H 10 20
I 20 25
A
B
C
(AA)
(AB)
(AC)
(A(BC))
((AB)C)
(((((DE)F)G)H)I)
(D(E(F(G(HI)))))
((D(EF))((GH)I))


Sample Output

0
0
0
error
10000
error
3500
15000
40500
47500
15125


// 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>

//存储输入的矩阵链条
char 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("%d", &n);

//input part one
for(t=1; t<=n; ++t)
{
getchar();
scanf("%c", &letter);
m = letter - 65;
Mtx[m].lter = letter;
scanf("%d", &Mtx[m].row);
scanf("%d", &Mtx[m].column);
}
memset(temp, '\0', sizeof(temp));

//input part two
while(scanf("%s", temp) != EOF)
{
for(i=0; i<1000; ++i)
stack[i].lter = '\0', stack[i].row = stack[i].column = 0;
num = flag = 0;
rear = front = 1;
len = strlen(temp);

//进栈各种情况判断
//其中stack[0]存储当前与下一矩阵进行运算的矩阵元素
for(i=0; i<len; ++i)
{
if(isalpha(temp[i]))
{
m = temp[i] - 65;
if(stack[0].lter == '\0')
{
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;
}
}
}
else if(temp[i] == '(')
{
if(stack[0].lter == '\0')
{
stack[rear++].lter = temp[i];
}
else
{
stack[rear++] = stack[0];
stack[rear++].lter = temp[i];
stack[0].lter = '\0';
}
}
else
{
rear--;
if(rear != front)
{
rear--;
if(stack[rear].lter != '(')
{
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++;

}
}
}
if(flag) printf("error\n");
else printf("%d\n", num);
}
return 0;
}

//Learn from it:
//定义的结构体变量可以用等号相互赋值
//字符变量实质上是一个字节的整型变量,可以把0~127之间的整数赋给一个字符变量
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: