Matrix Chain Multiplication 模拟栈

Matrix Chain Multiplication

Time Limit:1000MS  Memory Limit:32768K
Total Submit:36 Accepted:26

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

题意,判断某个输入序列的矩阵乘法是否合法,若合法,输出车乘法的次数,不合法输出error
分析:模拟栈的操作即可,建立两个栈,一个对应括号,一个对应矩阵,碰到‘ ( ’入栈,碰到‘)’出栈,再取矩阵栈顶的两个字母所代表的矩阵进行操作即可。
View Code
#include<stdio.h>
#include
<string.h>
#include
<string>
struct MAT{
int r,c;
}mat[
26],skm[100],a,b,c;
char name[26];
char stack[100];
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n,i,j;
char str[100],temp;
int top,top2,ans;
while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
{
scanf(
"%1s%d%d",&temp,&mat[i].r,&mat[i].c);
name[i]
=temp;
}
while(scanf("%s",str)!=EOF)
{
ans
=0;
top
=top2=0;
int len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]=='(')
stack[
++top]='(';
if(isalpha(str[i]))
{
for(j=1;j<=n;j++)
if(name[j]==str[i])
{
printf(
"%c\n",str[i]);
break;
}
skm[
++top2]=mat[j];
}
if(str[i]==')')
{
top
--;
a
=skm[top2--];c.c=a.c;
b
=skm[top2--];c.r=b.r;
if(b.c!=a.r)
{
printf(
"error\n");
break;
}
ans
+=b.r*b.c*a.c;
skm[
++top2]=c;
}
}
if(i==len)
printf(
"%d\n",ans);
}
}
return 0;
}

  也可以用c++的栈的写法,可读性更强

//注:懒得写,直接copy别人的
#include<iostream>
#include
<string>
#include
<stack>
using namespace std;
int answer;
typedef
struct
{
int rows;
int cols;
}Node;
Node matrix[
26];
char name[26];
int main()
{
int tCase,i,j;
int rows,cols;
string line;
stack
<char> skc;
stack
<Node> skm;
while(cin>>tCase)
{
for(i=1;i<=tCase;i++)
{
cin
>>name[i]>>rows>>cols;
matrix[i].cols
=cols;
matrix[i].rows
=rows;
}
while(cin>>line,!cin.eof())
{
answer
=0;
while(!skc.empty())
skc.pop();
while(!skc.empty())
skc.pop();
for(j=0;j!=line.size();++j)
{
if(line[j]=='(')
skc.push(
'(');
if(isalpha(line[j]))
{
for(i=1;i<=tCase;i++)
if(name[i]==line[j])
break;
skm.push(matrix[i]);
}
if(line[j]==')')
{
skc.pop();
Node c;
Node a
=skm.top();c.cols=a.cols;skm.pop();
Node b
=skm.top();c.rows=b.rows;skm.pop();
if(b.cols!=a.rows)
{
cout
<<"error"<<endl;
break;
}
answer
+=b.cols*b.rows*a.cols;
skm.push(c);
}
}
if(j==line.size())
cout
<<answer<<endl;
}
}
return 0;
}

  


你可能感兴趣的:(Matrix)