栈的练习,如此水题竟然做了两个小时。。。

题意:给出矩阵大小和矩阵的运算顺序,判断能否相乘并求运算量。

我的算法很简单:比如(((((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;
}//print
}//while
return 0;
}

提交时又忘记去掉文件重定向了,wa了一下。。。

UVA442 Matrix Chain Multiplication 矩阵运算量计算(栈的简单应用)的更多相关文章

  1. UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)

    意甲冠军  由于矩阵乘法计算链表达的数量,需要的计算  后的电流等于行的矩阵的矩阵的列数  他们乘足够的人才  非法输出error 输入是严格合法的  即使仅仅有两个相乘也会用括号括起来  并且括号中 ...

  2. UVa442 Matrix Chain Multiplication

    // UVa442 Matrix Chain Multiplication // 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.假定A和m*n的,B是n*p的,那么AB是m*p的,乘法 ...

  3. ACM学习历程——UVA442 Matrix Chain Multiplication(栈)

    Description   Matrix Chain Multiplication  Matrix Chain Multiplication  Suppose you have to evaluate ...

  4. UVa442 Matrix Chain Multiplication(栈)

    #include<cstdio>#include<cstring> #include<stack> #include<algorithm> #inclu ...

  5. 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 ...

  6. Matrix Chain Multiplication(表达式求值用栈操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...

  7. 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 ...

  8. Matrix Chain Multiplication[HDU1082]

    Matrix Chain Multiplication Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. UVA 442 二十 Matrix Chain Multiplication

    Matrix Chain Multiplication Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

随机推荐

  1. Activating Google Cloud Storage

    先决条件 你需要下面的内容: 1.一个Google账户,比如来自Gmail.如果你没有,请在Google account signup site注册. 2.一个新的或已经存在的Google Devel ...

  2. Codevs No.2144 砝码称重2

    2016-05-31 22:01:16 题目链接: 砝码称重2 (Codevs No.2144) 题目大意: 给定N个砝码,求称出M的重量所需砝码最小个数 解法: 贪心 使砝码数量最小,当然是每个砝码 ...

  3. 现代程序设计——homework-07

    1.写在前面 不得不很惭愧地说,在看这些博客之前,我对C++的了解仅限于上过一门特别水的关于C++的公选课.一门只有五节课的专业选修课,写过一点点符合C++语法语法规则的类C程序,偶尔在论坛.博客中看 ...

  4. homework-1

    看到这个题目开始我只能想到动态规划四个字,但具体采用什么方法,如何写成代码却还未成型.动态规划的典型特点就是利用之前的结果.于是我很快想到了之前一个比较典型的小程序,即求最长的连续字符串.这两个题目有 ...

  5. 【转】iOS 浅谈:深.浅拷贝与copy.strong

    深.浅拷贝 copy mutableCopy NSString 1 2 3 4 5 6 NSString *string = @"汉斯哈哈哈"; // 没有产生新对象 NSStri ...

  6. FIREDAC连接SQLITE乱码的解决

    在好多群里面都碰到问“FIREDAC连接SQLITE乱码的”的问题的同仁,遂将解决方法贴出来: 如上图所示设置 stringFormat为unicode即可

  7. Linux(Centos)全自动异地备份数据(WEB+Mysql)

    文章开始之前,先问下各位站长一个问题:什么东西对于站长是十分重要的?其实对于站长而言,很多东西都是很重要的.但我们现在排除外在因素,把范围缩小到网站系统本身,哪些是非常重要的呢?网站数据就是其中之一了 ...

  8. UVaLive 7363 A Rational Sequence (二叉树)

    题意:给定一个二叉树,并对每一个进行编号和规定,现在给你一个值,问你是第几个. 析:这个题,我想了好久才想出来,这个真是数据结构练的太差了,不够扎实,这个题,应该从下向上推,如果分子大于分母,那么这个 ...

  9. mac 下对 iterm 终端 设置代理

    vi .profile export http_prox="http://xxxx:port" export https_proxy="http://xxxx:port& ...

  10. Protobuf一键生成代码bat文件

    最近在摆弄Unity的Socket,需要用到Protobuf,一般都会有多个协议文件,所以研究了下bat的批处理,下面给出批处理文件代码: @echo off ::协议文件路径, 最后不要跟“\”符号 ...