UVA442 Matrix Chain Multiplication 矩阵运算量计算(栈的简单应用)
栈的练习,如此水题竟然做了两个小时。。。
题意:给出矩阵大小和矩阵的运算顺序,判断能否相乘并求运算量。
我的算法很简单:比如(((((DE)F)G)H)I),遇到 (就cnt累计加一,字母入栈,遇到)减一,并出栈两个矩阵计算运算量,将计算后的矩阵压入栈。当cnt等于0时就输出运算量。
难点是当不能运算后的处理。
卡那么就其实主要是细节问题,最大的坑是里面退栈时倒着退出,没注意到结果每次计算都判断为不能计算。。。
AC代码:
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
int const maxn = 27; struct Mat{
int x, y;
};
Mat mat[maxn]; Mat multip(Mat a, Mat b) {
Mat tmp;
tmp.x = a.x;
tmp.y = b.y;
return tmp;
} int main() {
int n, cnt = 0, kh = 0;
bool flag = true;
char tmp;
Mat a, b;
stack <Mat> v;
freopen("in", "r", stdin);
cin >> n;
while (n--) {
cin >> tmp;
tmp -= 'A';
cin >> mat[tmp].x >> mat[tmp].y;
}//while
while (cin >> tmp) {
if (kh == 0) {
flag = true;
}
if (flag == false) {
if (tmp == '(')
kh++;
else if (tmp == ')')
kh--;
continue;
}
if (tmp >= 'A' && tmp <= 'Z') {
v.push(mat[tmp - 'A']);
}//alpha
else{
if (tmp == '(') {
kh++;
}//(
else if (tmp == ')'){
kh--;
a = v.top();
v.pop();
b = v.top();
v.pop();
if (b.y != a.x) {
cout << "error" << endl;
cnt = 0;
flag = false;
continue;
}
cnt += b.x * a.x * a.y;
v.push(multip(b, a));
}//)
}//not alpha
if (kh == 0) {
cout << cnt << endl;
cnt = 0;
}//while
return 0;
}
提交时又忘记去掉文件重定向了,wa了一下。。。
UVA442 Matrix Chain Multiplication 矩阵运算量计算(栈的简单应用)的更多相关文章
- UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)
意甲冠军 由于矩阵乘法计算链表达的数量,需要的计算 后的电流等于行的矩阵的矩阵的列数 他们乘足够的人才 非法输出error 输入是严格合法的 即使仅仅有两个相乘也会用括号括起来 并且括号中 ...
- UVa442 Matrix Chain Multiplication
// UVa442 Matrix Chain Multiplication // 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.假定A和m*n的,B是n*p的,那么AB是m*p的,乘法 ...
- ACM学习历程——UVA442 Matrix Chain Multiplication(栈)
Description Matrix Chain Multiplication Matrix Chain Multiplication Suppose you have to evaluate ...
- UVa442 Matrix Chain Multiplication(栈)
#include<cstdio>#include<cstring> #include<stack> #include<algorithm> #inclu ...
- uva-442 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 ma ...
- Matrix Chain Multiplication(表达式求值用栈操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...
- UVA——442 Matrix Chain Multiplication
442 Matrix Chain MultiplicationSuppose you have to evaluate an expression like A*B*C*D*E where A,B,C ...
- Matrix Chain Multiplication[HDU1082]
Matrix Chain Multiplication Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- UVA 442 二十 Matrix Chain Multiplication
Matrix Chain Multiplication Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
随机推荐
- Hadoop中FileSystem的append方法
今天在使用Hadoop 1.1.2版本进行FileSystem的append操作时报以下异常: org.apache.hadoop.ipc.RemoteException: java.io.IOExc ...
- 轻松突击ThreadLocal
本文出自 代码大湿 代码大湿 ThreadLocal是用来保存线程的本地变量,可以保证每个线程都有一个自己的变量(包括static变量). 本文所有代码请点击我 1 看个实际场景. 我们要设计一个序列 ...
- jira部署,主机迁移,数据库迁移,jira
1,linux环境下快速部署; wget http://wpc.29c4.edgecastcdn.net/8029C4/downloads/software/jira/downloads/atlass ...
- 快速切换目录软件推荐——autojump
受到<autojump: 在命令行下快速更改目录>的鼓动,决定试用下这个软件. 但ubuntu下的源貌似有些问题, sudo apt get install autojump 后,死活提示 ...
- Android问题-DelphiXE8新建AVD出现“no system images installed for this target”
相关资料: 1.http://www.cnblogs.com/yc-755909659/p/4080645.html 问题现象:创建Android模拟器提不”no system images inst ...
- VIM技巧(2)-删除匹配行
VIM技巧(2)-删除匹配行 代码如下: * @Company:中国股份有限公司 * @author ymzhao (也有zyyang的) * @Date: Jan 22, 2014 11:25:29 ...
- bzoj 2190 仪仗队(欧拉函数)
2190: [SDOI2008]仪仗队 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2245 Solved: 1413[Submit][Statu ...
- jQuery基础学习3——jQuery库冲突
默认情况下,jQuery用$作为自身的快捷方式. jQuery库在其他库之后导入 在其他库和jQuery库都被加载完毕后,可以在任何时候调用jQuery.noConflict()函数来将变量$的控制权 ...
- JAVA 反射应用:properties2Object
MixAll.java import java.lang.reflect.Method; import java.util.Properties; public class MixAll { /** ...
- jQuery 源码解析二:jQuery.fn.extend=jQuery.extend 方法探究
终于动笔开始 jQuery 源码解析第二篇,写文章还真是有难度,要把自已懂的表述清楚,要让别人听懂真的不是一见易事. 在 jQuery 源码解析一:jQuery 类库整体架构设计解析 一文,大致描述了 ...