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 ...
随机推荐
- Layout No collapsible
center 不可折叠 其它的,没有 title 没法折叠 title 使用子控件的
- cocos2d-x 纹理深入研究 第二部分
转自:http://blog.csdn.net/qq51931373/article/details/9152227 1.纹理控制. 看此代码: CCSprite *pSprite = CCSprit ...
- Java缓存学习之三:CDN缓存机制
CDN是什么? 关于CDN是什么,此前网友详细介绍过. CDN是Content Delivery Network的简称,即"内容分发网络"的意思.一般我们所说的CDN加速,一般是指 ...
- JSF 2 multiple select dropdown box example
In JSF, <h:selectManyMenu /> tag is used to render a multiple select dropdown box – HTML selec ...
- C#类、静态类、静态变量,初始化执行顺序
执行顺序: 类的静态变量 ↓ 类的静态构造函数 ↓ 类的普通变量 ↓ 基类的静态变量 ↓ 基类的静态构造函数 ↓ 基类的普通变量 ↓ 基类的构造函数 ↓ 类的构造函数
- [iOS微博项目 - 1.6] - 自定义TabBar
A.自定义TabBar 1.需求 控制TabBar内的item的文本颜色(普通状态.被选中状态要和图标一致).背景(普通状态.被选中状态均为透明) 重新设置TabBar内的item位置,为下一步在Ta ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- POJ2001Shortest Prefixes(字典树)
题目大意就是帮你给N条字符串,每条长度不超过20.问要将他们单一识别出来,每个字符串最短可以缩为多短. 如: abc abcdefg bc adef 这四个字符串就可分别缩写为 abc abcd b ...
- hash_map vs unordered_map vs map vs unordered_set
hash_map vs unordered_map 这两个的内部结构都是采用哈希表来实现.unordered_map在C++11的时候被引入标准库了,而hash_map没有,所以建议还是使用unord ...
- MVC3 Razor模板引擎
http://blog.csdn.net/tiz198183/article/details/8659362 一.Views文件夹 -> Shared文件夹下的 _Layout.cshtml 母 ...