杭电acm—1082 Matrix Chain Multiplication

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082

Matrix Chain Multiplication

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1891    Accepted Submission(s): 1222

 

 

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

 

 

Source

University of Ulm Local Contest 1996

 

这个题目,弄懂矩阵相乘就行了,不了解的朋友,可以百度一下,说白了,这个题目就是抓住三点,假设A:3行2列,B:2列三行,两个矩阵,第一点:A*B,一共乘的次数为:A的行乘以B的行列,即:3*2*3,第二点:能相乘的条件是,A的列等于B的行,第三点:假设A*B=C,那么C的行,为A的行,列,为B的列。自己在本子上面推算一下吧。

AC代码如下:(如有错误和疑问,请各位不吝指出)

#include<stdio.h>
#include<string.h>
int hgs[30][2];
int s[1000][2];
int main(){
	int n;
	char str[1000]; 
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		char c;
		int x,y;
		getchar();
		scanf("%c%d%d",&c,&x,&y);
		hgs[c-'A'][0]=x;
		hgs[c-'A'][1]=y;
	}
	getchar();
	while(scanf("%s",str)!=EOF){
		int length=strlen(str);
		int sum=0,k=0,flag=0;
		if(length==1)
		  printf("0\n");
		else{
			for(int i=0;i<length;i++){
				if(str[i]=='(')
				  continue;
				if(str[i]==')'){//一旦出现')',意味着有两个要相乘 
					if(s[k-2][1]!=s[k-1][0]){//判断是否可以相乘 
						flag=1;
						break;
					}
					sum+=s[k-2][0]*s[k-1][0]*s[k-1][1];//A的行乘以B的行和列 
					s[k-2][1]=s[k-1][1];//注意把B的列赋给A的列 
					k--;//A乘B得到A,所以k-- 
				}else{
					s[k][0]=hgs[str[i]-'A'][0];
					s[k][1]=hgs[str[i]-'A'][1];
					k++;
				}
			}
			if(flag)
			  printf("error\n");
			else
			  printf("%d\n",sum);
		}
	}
	return 0;
}

上面的做法主要是字符串处理,加条件判断,但是这很麻烦,如果用栈来做的话,又是另一番风景了。简单明朗许多。

下面这是栈的ac代码:

#include<cstdio>
#include<stack>
#include<iostream>
#include<string>
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;
	for(int i=0; i<n; i++) {
		string name;
		cin>>name;
		int k=name[0]-'A';
		cin>>m[k].a>>m[k].b;
	}
	string expr;
	while(cin>>expr){
		int len=expr.length();
		bool error=false;
		int ans=0;
		for(int i=0;i<len;i++){
			if(isalpha(expr[i]))  s.push(m[expr[i]-'A']);
			else if(expr[i]==')'){
				Matrix m2=s.top();  s.pop();
				Matrix m1=s.top();  s.pop();
				if(m1.b!=m2.a){
					error=true;  break;
				}
				ans+=m1.a*m1.b*m2.b;
				s.push(Matrix(m1.a,m2.b));
			} 
		}
		if(error)  printf("error\n");
		else  printf("%d\n",ans);
	}
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值