题目传送门

题意:给出每个矩阵的行列,计算矩阵的表达式,如果错误输出error,否则输出答案

分析:表达式求值,stack 容器的应用:矩阵的表达式求值A 矩阵是a * b,B 矩阵是b * c,则A * B 是a * c。遇到')'弹出两个矩阵相乘,错误的话直接break

收获:以前做过了,现在会表达式求值后,这题也太容易了

代码:

  1. /************************************************
  2. * Author :Running_Time
  3. * Created Time :2015-8-29 10:22:51
  4. * File Name :UVA_442.cpp
  5. ************************************************/
  6.  
  7. #include <cstdio>
  8. #include <algorithm>
  9. #include <iostream>
  10. #include <sstream>
  11. #include <cstring>
  12. #include <cmath>
  13. #include <string>
  14. #include <vector>
  15. #include <queue>
  16. #include <deque>
  17. #include <stack>
  18. #include <list>
  19. #include <map>
  20. #include <set>
  21. #include <bitset>
  22. #include <cstdlib>
  23. #include <ctime>
  24. using namespace std;
  25.  
  26. #define lson l, mid, rt << 1
  27. #define rson mid + 1, r, rt << 1 | 1
  28. typedef long long ll;
  29. const int N = 1e5 + 10;
  30. const int INF = 0x3f3f3f3f;
  31. const int MOD = 1e9 + 7;
  32. struct Martrix {
  33. int a, b;
  34. Martrix (int _a = 0, int _b = 0) : a (_a), b (_b) {};
  35. }m[26];
  36.  
  37. int main(void) {
  38. int n;
  39. string name;
  40. cin >> n;
  41. for (int i=1; i<=n; ++i) {
  42. cin >> name; int k = name[0] - 'A';
  43. cin >> m[k].a >> m[k].b;
  44. }
  45.  
  46. string exp;
  47. stack<Martrix> S;
  48. while (cin >> exp) {
  49. bool error = false;
  50. int ans = 0;
  51. int len = exp.length ();
  52. for (int i=0; i<len; ++i) {
  53. if ('A' <= exp[i] && exp[i] <= 'Z') S.push (m[exp[i]-'A']);
  54. else if (exp[i] == ')') {
  55. Martrix m1 = S.top (); S.pop ();
  56. Martrix m2 = S.top (); S.pop ();
  57. if (m2.b != m1.a) {
  58. error = true; break;
  59. }
  60. ans += m2.a * m2.b * m1.b;
  61. S.push (Martrix (m2.a, m1.b));
  62. }
  63. }
  64. if (error) cout << "error" << endl;
  65. else cout << ans << endl;
  66. }
  67.  
  68. return 0;
  69. }

stack UVA 442 Matrix Chain Multiplication的更多相关文章

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

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

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

  3. UVa 442 Matrix Chain Multiplication(栈的应用)

    题目链接: https://cn.vjudge.net/problem/UVA-442 /* 问题 输入有括号表示优先级的矩阵链乘式子,计算该式进行的乘法次数之和 解题思路 栈的应用,直接忽视左括号, ...

  4. UVA - 442 Matrix Chain Multiplication(栈模拟水题+专治自闭)

    题目: 给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数. 思路: 遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的 ...

  5. 例题6-3 Matrix Chain Multiplication ,Uva 442

    这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试 得到的经验有以下几点: 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习 有时候代码不对,得到的结果却是对的(之后总结以下常见 ...

  6. UVA 442 二十 Matrix Chain Multiplication

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

  7. UVa442 Matrix Chain Multiplication

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

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

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

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

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

随机推荐

  1. 实战 -- Redis2.4.2集成spring3.2.2

    redis.host=... redis.port= redis.pass= redis.timeout= #最大能够保持idel状态的对象数 redis.maxIdle= #最大分配的对象数 red ...

  2. php面试题之三——PHP语言基础(基础部分)

    三.PHP语言基础 1. strlen( )与 mb_strlen( )的作用分别是什么(新浪网技术部) strlen和mb_strlen都是用于获取字符串长度. strlen只针对单字节编码字符,也 ...

  3. 我的grub.cfg配置文件

    路径:/boot/grub/grub.cfg 配置文件如下: # # DO NOT EDIT THIS FILE # # It is automatically generated by grub-m ...

  4. 使用Unity创造动态的2D水体效果

    者:Alex Rose 在本篇教程中,我们将使用简单的物理机制模拟一个动态的2D水体.我们将使用一个线性渲染器.网格渲染器,触发器以及粒子的混合体来创造这一水体效果,最终得到可运用于你下款游戏的水纹和 ...

  5. IOS model的getter和setter方法

    总结: 当使用 self.str1 = @"xxx";时, 系统自动调用 setter方法 param_str = self.str1; 自动调用getter方法注意: 只在对象点 ...

  6. ref游标(动态游标)

    参照变量--用于存放数值指针的变量    游标变量(ref cursor)   使用游标时,当定义游标时不需要指定相应的select语句,但是当使用    游标时(open时)需要指定select语句 ...

  7. Ubuntu删除history记录

    history -c就是清除本次登录到目前所执行的命令 转自: http://www.linuxdiyf.com/viewarticle.php?id=189355

  8. c++标准库中几个常见的数据结构的区别和应用规则

    转载自http://www.lifecrunch.biz/archives/202 vector和built-in数组类似,它拥有一段连续的内存空间,并且起始地址不变,因此它能非常好的支持随即存取,即 ...

  9. php中正则表达式的匹配和数据验证总结

    正则表达式能匹配复杂的字符串形式,比字符串处理函数功能更加多,只不过执行效率有所降低,但是可以实现非常复杂的匹配,下面总结一下 1.简单的字符串匹配,判断指定字符串是不是在另一个字符串中,和字符串查找 ...

  10. Java for LeetCode 150 Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...