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;


}

算法之动态规划-矩阵链相乘(matrix-chain multiplication)

Matrix-chain multiplication给定一串矩阵 A1,A2...AnA1,A2...An,计算矩阵的值:A1A2A3..AnA_1A_2A_3..A_n。对于这串矩阵序列,不同的加...
  • qq_24145735
  • qq_24145735
  • 2016年04月07日 22:38
  • 1130

【动态规划DP,二维动归】poj1651,Multiplication Puzzle

http://poj.org/problem?id=1651 有N张写有数字的卡片排成一行,按一定次序从中拿走N-2张(第1张和最后一张不能拿),每次只拿一张,取走一张卡片的同时,会得到一个...
  • mmc2015
  • mmc2015
  • 2015年08月06日 19:46
  • 1409

POJ2155:Matrix(二维树状数组,经典)

Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in ...
  • libin56842
  • libin56842
  • 2015年06月24日 12:17
  • 2605

POJ 2246 Matrix Chain Multiplication 栈

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

【日常学习】【栈】【表达式求值】Uva442 - Matrix Chain Multiplication题解

之前一直没有写过栈的典型程序,这里写一个。这个程序完全是我独立写出来的,我还没有看ruka上的标程,或许会有些不同。 题目来源:University of Ulm Local Contest 1996...
  • ametake
  • ametake
  • 2015年02月26日 14:33
  • 612

ZOJ1094Matrix Chain Multiplication

没找到分类,简单题。在函数的返回值上, 和如何定位left和right上纠结了一下。最初纳闷了一下如何让字母和x、y关联,查了下竟然有这么简单的方法。 //1094Matrix Chai...
  • zillion217
  • zillion217
  • 2011年10月02日 20:55
  • 179

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 ...
  • cscj2010
  • cscj2010
  • 2012年03月14日 02:40
  • 487

UVa 442 ------ Matrix Chain Multiplication

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

Uva 442 Matrix Chain Multiplication (矩阵连乘)

Matrix Chain Multiplication Time Limit:Unknown   Memory Limit:Unknown   64bit IO Format...
  • HelloWorld10086
  • HelloWorld10086
  • 2014年07月22日 11:21
  • 665

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

442 - Matrix Chain Multiplication 5134 59.82% 2559 92.93% ...
  • shuangde800
  • shuangde800
  • 2012年07月01日 07:16
  • 2075
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj Matrix Chain Multiplication
举报原因:
原因补充:

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