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 ...
随机推荐
- ACM竞赛 Java编程小结
1.字符串的长度 String str = new String(" abcd"); int length = str.length(); 2.数组的长度.排序 2.1对于 a[] ...
- java工程师的标准
1.技术广度方面至少要精通多门开源技术吧,研究过struts\spring\hibernate等的源码. 2.项目经验方面从头到尾跟过几个大项目,头是指需求阶段,包括需求调研.尾是指上线交付之后,包括 ...
- Spark SQL概念学习系列之Spark SQL 架构分析(四)
Spark SQL 与传统 DBMS 的查询优化器 + 执行器的架构较为类似,只不过其执行器是在分布式环境中实现,并采用的 Spark 作为执行引擎. Spark SQL 的查询优化是Catalyst ...
- JavaScript——对this指针的新理解
一直以来对this的理解只在可以用,会用,却没有去深究其本质.这次,借着<JavaScript The Good Parts>,作了一次深刻的理解.(所有调试都可以在控制台中看到,浏览器F ...
- 获取iOS设备信息(内存/电量/容量/型号/IP地址/当前WIFI名称)
1.获取电池电量(一般用百分数表示,大家自行处理就好) 1 2 3 4 -(CGFloat)getBatteryQuantity { return [[UIDevice current ...
- HDU 5744 Keep On Movin (贪心)
Keep On Movin 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5744 Description Professor Zhang has k ...
- UVALive 6910 Cutting Tree(离线逆序并查集)
[题目]:(地址:) http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97671#problem/E [题意]: 给出多棵树和两类操作:操作 ...
- 通过源码学Java基础:InputStream、OutputStream、FileInputStream和FileOutputStream
1. InputStream 1.1 说明 InputStream是一个抽象类,具体来讲: This abstract class is the superclass of all classes r ...
- jQuery基础学习3——jQuery库冲突
默认情况下,jQuery用$作为自身的快捷方式. jQuery库在其他库之后导入 在其他库和jQuery库都被加载完毕后,可以在任何时候调用jQuery.noConflict()函数来将变量$的控制权 ...
- HDU 4461 The Power of Xiangqi (水题)
题意:给定一些字母,每个字母都代表一值,如果字母中没有B,或者C,那么就在总值大于1的条件下删除1,然后比较大小. 析:没什么好说的,加起来比较就好了. 代码如下: #pragma comment(l ...