poj Matrix Chain Multiplication

来源:互联网 发布:网络运维服务合同 编辑:程序博客网 时间:2024/06/12 01:13

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

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


/*

本题思路: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;


}