hdu1082 Matrix Chain Multiplication

39 篇文章 0 订阅
35 篇文章 0 订阅
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
 

Recommend
We have carefully selected several similar problems for you:   1074  1081  1080  1505  1069 
 
分析:
       这里假设输入的括号是合法的,因此,程序里面不对输入匹配性做合法检验,程序里面主要检测矩阵相乘是否合法,比如,一个10X20的矩阵无法与30X10的矩阵相乘,所以,在此使用一个堆栈,从左到右分析输入串,碰到'('直接忽略,碰到')'则从栈里面弹出两个矩阵,再相乘(如果相乘不合法,则输出不合法,并返回),将相乘的结果压入堆栈,一直到扫描完输入串,此时再扫描堆栈。
代码如下所求:
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

typedef struct Record{
	int m;
	int n;
}Record;

Record r[100];
vector<Record> stk;

void Init();
void Caculate(char* buf);

int main(){
	Init();
	char buf[10000];
	while(scanf("%s", buf) != EOF){
		Caculate(buf);
	}
}

void Init(){
	int len;
	scanf("%d", &len);
	char c;
	for(int i = 0; i < len; ++i){
		scanf("%c %c", &c, &c);
		c -= 'A';
		scanf("%d %d", &r[c].m, &r[c].n);
	}
}

void Caculate(char* buf){
	int l = 0, r = 0, result = 0, len = strlen(buf);
	stk.clear();
	char c;
	for(int i = 0; i < len; ++i){
		c = buf[i];
		if(c == '('){
			++l;
		}else if(c == ')'){
			--l;
			if(stk.size() >= 2){
				Record lr = stk.back();
				stk.pop_back();
				Record ll = stk.back();
				stk.pop_back();
				if(lr.m != ll.n){
					printf("error\n");
					return;
				}else{
					Record r;
					r.m = ll.m;
					r.n = lr.n;
					result += ll.m * ll.n * lr.n;
					stk.push_back(r);
				}
			}else if(stk.size() == 1){
				//do nothing
			}else{//0
				printf("error\n");
				return;
			}
		}else{//push stack
			stk.push_back(::r[c-'A']);
		}
	}
	Record lr;//= stk.back();
	//stk.pop_back();
	Record ll;// = stk.back();
	//stk.pop_back();
	Record rr;
	while(stk.size() > 1){
		lr = stk.back();
		stk.pop_back();
		ll = stk.back();
		stk.pop_back();
		if(ll.n != lr.m){
			printf("error\n");
			return;
		}else{
			rr.m = ll.m;
			rr.n = lr.n;
			stk.push_back(rr);
			result += ll.m * ll.n * lr.n;
		}
	}
	printf("%d\n", result);
}

提交结果如下所求:
Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
155960682015-11-21 23:45:33Accepted10820MS1724K1603 BC++BossJue

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值