poj Matrix Chain Multiplication

原创 2012年04月02日 12:55:31

Matrix Chain Multiplication

Time Limit:1000MS  Memory Limit:65536K
Total Submit:12 Accepted:6

Description

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

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 (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 }
Line = Expression
Expression = Matrix | "(" Expression Expression ")"
Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"

Output

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


/*

本题思路:if 测试数据时单个元素时输出0;
          else 如果遇到'('continue;
      如果遇到A……Z 进栈;
  如果遇到')'取出栈顶的两个元素作相应的计算 把计算结果进栈;
  知道到结束;
*/
#include"stdio.h"
#include"string.h"
struct node1
{
int x,y;
}point[27];
typedef struct node
{
int ab[1000][2];//存储进栈元素;
int top;//栈顶元素的下标;
}point1;
int main()
{
int i,n;
scanf("%d",&n);
getchar();
char ch;
int x1,y1;
for(i=0;i<n;i++)
{
scanf("%c %d %d",&ch,&x1,&y1);
point[ch-'A'].x=x1;
point[ch-'A'].y=y1;
getchar();
}
char s[1000];
while(gets(s))

int f=1;
   int num=0;
   int len=strlen(s);
if(len==1)
{
printf("0\n");
continue;
}
point1 cur;
cur.top=-1;
int x2,y2,x3,y3;
for(i=0;i<len;i++)
{
if(s[i]=='(')
continue;
if(s[i]>='A'&&s[i]<='Z')
{
cur.top++;
cur.ab[cur.top][0]=point[s[i]-'A'].x;
cur.ab[cur.top][1]=point[s[i]-'A'].y;//进栈;
}
if(s[i]==')'||s[i+1]=='\0')
{
           x2=cur.ab[cur.top][0];
  y2=cur.ab[cur.top][1];//出栈;
  cur.top--;//栈顶的下标也要相应的减一;
  x3=cur.ab[cur.top][0];
  y3=cur.ab[cur.top][1];//出栈;
  cur.top--;
  if(x2!=y3)
  {f=0;break;}//如果前一个矩阵的列和后一个矩阵的行不相等的话就跳出;
  num+=x2*y2*x3;//计算基本相乘元素的个数;
  cur.top++;
  cur.ab[cur.top][0]=x3;
  cur.ab[cur.top][1]=y2;//结果进栈;
}
}
if(!f)
printf("error\n");
else printf("%d\n",num);
}
return 0;


}

相关文章推荐

POJ 2246 Matrix Chain Multiplication 栈

Matrix Chain MultiplicationTime Limit: 1000MS Memory Limit: 65536K Total Submissions: 2416 ...
  • dl2hq
  • dl2hq
  • 2017年03月13日 20:10
  • 101

hdu 1082 Matrix Chain Multiplication

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

UVa 442 ------ Matrix Chain Multiplication

题目: UVa 442 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的步数。若乘法无法进行,输出error。     如A m*n, B n*p, 那么A*B的步数为m*n*...

Uva 442 Matrix Chain Multiplication (矩阵连乘)

Matrix Chain Multiplication Time Limit:Unknown   Memory Limit:Unknown   64bit IO Format...

UVA 442 - Matrix Chain Multiplication 数据结构专题

442 - Matrix Chain Multiplication 5134 59.82% 2559 92.93% ...

Matrix Chain Multiplication UVA442 矩阵连乘 stack

题目链接 http://www.bnuoj.com/v3/problem_show.php?pid=17581 Matrix Chain Multiplication  S...

UVA_442: Matrix Chain Multiplication

Description Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matri...

Geeks面试题:Matrix Chain Multiplication

Matrix Chain Multiplication Given a sequence of matrices, find the most efficient way to multipl...

矩阵链乘(Matrix Chain Multiplication)

Matrix Chain Multiplication Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:...

Matrix Chain Multiplication

Description Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matri...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj Matrix Chain Multiplication
举报原因:
原因补充:

(最多只允许输入30个字)