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-Map/Reduce实现实现倒排索引
先来简单介绍一下什么是文档倒排索引 倒排索引是文档检索系统中最常见的数据结构,被广泛应用在全文搜索引擎上.主要用来存储某个单词(或词组)在一个文档或者一组文档中的存储位置的映射,即提供了一种根据内容来 ...
- iOS开发中使用Bmob RESTful API
简介 尽管Bmob已经提供了一套SDK供开发者使用,但有时候开发者可能希望能直接与Bmob后台进行直接交互,以达到某些特别的需求(直接操作_User表.同步网络请求等).而RESTful API可以使 ...
- RTMP、RTSP、HTTP视频协议详解(转)
一.RTMP.RTSP.HTTP协议 这三个协议都属于互联网 TCP/IP 五层体系结构中应用层的协议.理论上这三种都可以用来做视频直播或点播.但通常来说,直播一般用 RTMP.RTSP.而点播用 H ...
- Linux rpm 命令参数
rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种.二进制包可以直接安装在计算机中,而源代码包将会由RPM自动编译.安装.源代码包经常以src.rpm作为后缀名. 常用命令组合 ...
- Linux 获取文件时间信息 判断文件是否存在
获取文件时间戳 (1)查看全部信息: stat e4.txt 范例: [root@localhost ~]# stat e4.txt File: “e4.txt” Size: 0 Blocks: ...
- Beginning OpenGL ES 2.0 with GLKit Part 1
Update 10/24/12: If you’d like a new version of this tutorial fully updated for iOS 6 and Xcode 4.5, ...
- Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)
题目链接:http://codeforces.com/problemset/problem/598/D 题意是 给你一张行为n宽为m的图 k个询问点 ,求每个寻问点所在的封闭的一个上下左右连接的块所能 ...
- 亲和串(HDU2203)
http://acm.hdu.edu.cn/showproblem.php?pid=2203 题目意思很简单,求s1串所构成的环中是否有s2这个串 用CMP参考http://s.acmore.net/ ...
- Python if..else
200 ? "200px" : this.width)!important;} --> 一.介绍 1.完整形式 if <条件判断1>: <执行1> e ...
- c++的操作符格式记录
以下摘自维基百科,mark一下,以备不时之需. For the purposes of this table, a, b, and c represent valid values (literals ...