UVaOJ442---Matrix Chain Multiplication
来源:互联网 发布:阿里妈妈淘宝卖家 编辑:程序博客网 时间:2024/06/11 15:46
442 - Matrix Chain Multiplication
Time limit: 3.000 seconds
442 - Matrix Chain Multiplication
Time limit: 3.000 secondsSuppose 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 Specification
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 ( ), 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 Specification
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
#include <iostream>#include <string>#include <cstdio>#include <sstream>#include <stack>using namespace std;typedef struct{ int r, c;}Matrix;Matrix m[26];void slove (string st){ stack<char> sc; stack<Matrix> sm; stringstream ss(st); char ch; Matrix a, b, c; int sum = 0; while (ss>>ch) { if (ch == ')') { a = sm.top(); sm.pop(); b = sm.top(); sm.pop(); if (b.c != a.r) { cout<<"error"<<endl; return; } sum += b.r * b.c * a.c; c.r = b.r; c.c = a.c; sm.push(c); sc.pop(); } else if (ch == '(') { sc.push(ch); } else { a = m[ch - 'A']; sm.push(a); } } cout<<sum<<endl;}int main(){ int n; char c; cin>>n; for (int i = 0; i < n; i ++) { cin>>c; cin>>m[c - 'A'].r>>m[c - 'A'].c; } string st; getline(cin, st); while (getline(cin, st)) { slove(st); } return 0;}
- UVaOJ442---Matrix Chain Multiplication
- Matrix Chain Multiplication
- zoj1094 Matrix Chain Multiplication
- Matrix chain multiplication problem
- Matrix Chain Multiplication
- uva442 Matrix Chain Multiplication
- 442 - Matrix Chain Multiplication***
- poj Matrix Chain Multiplication
- 442 - Matrix Chain Multiplication
- zoj1094-Matrix Chain Multiplication
- poj2246 - Matrix Chain Multiplication
- 442 - Matrix Chain Multiplication
- uva442 Matrix Chain Multiplication
- 442 - Matrix Chain Multiplication
- 442 - Matrix Chain Multiplication
- ZOJ1094-Matrix Chain Multiplication
- Matrix Chain Multiplication uva
- UVA Matrix Chain Multiplication
- 为何要学习MFC?
- 使用Spring连接mysql数据库
- 编程之美-2.3、寻找发帖水王id占一半
- Oracle收集用户的权限
- 单向链表逆序
- UVaOJ442---Matrix Chain Multiplication
- 黑马程序员_JavaScript函数的基本使用
- nandflash下通过supervivi命令行实现挂载root_qtopia的NFS挂载
- 锁存器
- 你们的软件工程师面试都弱爆了
- DFS-变形课
- Dalvik中类的加载
- php 文件下载 出现下载文件内容乱码损坏的解决方法
- UVaOJ11111---Generalized Matrioshkas