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

HDU 1082 Matrix Chain Multiplication(堆栈的简单运用)

2016-08-01 21:01 453 查看
Problem 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 } <EOF>

Line = Expression <CR>

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

#include <iostream>
#include <stack>
using namespace std;
struct Matrix{
int a,b;
Matrix(int a=0, int b=0):a(a),b(b){
}
}m[26];
stack<Matrix> s;
int main(){
int n;
cin>>n;
while(n--){
char c;
cin>>c;
cin>>m[c-'A'].a>>m[c-'A'].b;//考虑乱序输入的情况
}

string mx;

while(cin>>mx){

int l=mx.length();//注意三个变量的位置
bool error=0;
int sum=0;

for(int i=0;i<l;i++){
if(isalpha(mx[i])){
s.push(m[mx[i]-'A']);
}
else if(mx[i]==')'){
Matrix x=s.top(); //先进后出 x=B y=A
s.pop();
Matrix y=s.top();
s.pop();
if(x.a!=y.b){
error=1;
break;
}
else{
sum+=(y.a*y.b*x.b);
s.push(Matrix(y.a,x.b));
}
}
}
if(error) cout<<"error"<<endl;
else cout<<sum<<endl;

}

}


题目的意思是:有N个已知长度宽度的矩阵,求给出的矩阵是否能相乘,如果能就求出相乘次数,反之输出“error”;

首先建立一个矩阵的结构体,储存矩阵的长度和宽度;

然后利用栈先进后出的思路,在遇到字母时储存,遇到“)”时就判断:如果A矩阵的宽度=B矩阵的长度,则输出相乘结果,并把A矩阵的长度和B矩阵的宽度作为新矩阵;不能则直接break掉输出error;

输入的时候有个小技巧,输入的字符位置为[c-'A'],这样就能把字母按顺序存在数组里;

判断时要注意是B先出,A后出,所以x为B矩阵,y为A矩阵;

新矩阵可以直接创建一个结构体类型变量,也可以用结构体的构造函数(看上去很6);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: