UVA 442(栈的应用)
来源:互联网 发布:姚明nba打球平均数据 编辑:程序博客网 时间:2024/05/17 03:06
Description
Matrix Chain Multiplication
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 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
题意给出个n个矩阵的行和列,按照括号优先级的顺序进行矩阵乘法,计算总的乘法次数,不能相乘的输出错误。
遇到字母直接入栈,遇到右括号就计算。
#include<stdio.h>#include<stack>#include<algorithm>#include<cctype>#include<string.h>int m[30],n[30];char s[1005],c;int main(){ using std::stack; int t; //freopen("in.txt","r",stdin); scanf("%d",&t); for(int i=1; i<=t; i++) { scanf("%s",&c); scanf("%d%d",&m[c-'A'],&n[c-'A']); } getchar(); while(gets(s)!=NULL) { stack<int>M,N; int len=strlen(s),ans=0,lose=0; for(int i=0; i<len; i++) { if(s[i]==')') { int u=M.top(),v=N.top(); M.pop(); N.pop(); if(!M.empty()) { if(N.top()!=u) { lose=1; break; } else { ans+=M.top()*N.top()*v; N.pop(); N.push(v); } } } if(isalpha(s[i])) { M.push(m[s[i]-'A']); N.push(n[s[i]-'A']); } } int am,bm=-1,an,bn=-1;//判断不含括号的情况 if(!M.empty()) { am=M.top(); M.pop(); an=N.top(); N.pop(); } if(!M.empty()) { bm=M.top(); bn=N.top(); } if(bm!=-1) { if(an!=bm)lose=1; else ans+=am*an*bn; } if(!lose)printf("%d\n",ans); else puts("error"); } return 0;}
- UVA 442(栈的应用)
- UVA 673 栈的应用
- UVA - 673 - Parentheses Balance (栈的应用!)
- UVA - 11624 - Fire! (BFS的应用)
- Uva 514 Rails(栈应用)
- uva 610(tarjan的应用)
- UVA - 112 - Tree Summing (数的求和!栈的应用!)
- 经典第六章 例题 6-2 UVA 514 Rails(栈的基础应用)
- uva 1428 - Ping pong (树状数组的应用)
- Uva 11020 Efficient Solutions (set的应用)
- UVA 11488 Hyper Prefix Sets(trie的应用)
- UVA 12712 Pattern Locker(排列的基本应用)
- uva 247 Calling Circles(Floyd 的简单应用)
- UVA 10420-List of Conquests(STL-map的应用)
- UVA - 11078 - Open Credit System (高效算法的应用!!)
- uva 11300 Spreading the Wealth (中位数的应用)
- 求期望,对数的应用,预处理(糖果,uva 1639)
- UVA 6859 Points(凸包的应用)
- 如何让笔记本变成wifi热点
- cocos2d-x: 33种切换场景
- 如何让spring开发中beans.xml出现属性提示
- sam 百度面经的问题总结(2) 性能优化
- C语言拾遗:常量指针与指针常量(const int *ptr, int const *ptr, int * const ptr, const int * const ptr区别)
- UVA 442(栈的应用)
- 重装windows后grub2修复
- 《剑指offer》学习之--顺时针打印矩阵
- Java中覆盖、重写、重载详解
- 偷了世界的程序员
- linux服务器本地和百度云备份脚本小试
- WebService到底是什么
- JS图片无缝滚动
- Android_Lesson01