题目传送门

题意:给出每个矩阵的行列,计算矩阵的表达式,如果错误输出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. App架构设计经验谈:服务端接口的设计

    App与服务器的通信接口如何设计得好,需要考虑的地方挺多的,在此根据我的一些经验做一些总结分享,旨在抛砖引玉. 安全机制的设计 现在,大部分App的接口都采用RESTful架构,RESTFul最重要的 ...

  2. Unity3D模型的细致纹理问题解决办法

    http://hunterwang.diandian.com/post/2012-09-28/40039798509 也许有人也遇到过同样的问题,也许解决方式不同,我来介绍一下偶尔尝试发现的解决办法. ...

  3. compact过滤数组中的nil

    http://ruby-doc.org/core-2.2.0/Array.html#method-i-compact compact → new_aryclick to toggle source R ...

  4. 第3章 K近邻法

    参考: http://www.cnblogs.com/juefan/p/3807713.html http://blog.csdn.net/v_july_v/article/details/82036 ...

  5. 不用任何图片,只用简单的css写出唯美的钟表,就问你行吗?

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAGHCAIAAABJR31QAAAgAElEQVR4nOy9aXhc1ZUurPvcH7f73n ...

  6. Firefox上Web开发工具库一览

    Firefox的目标之一就是尽可能地使web开发者的生活更简单高效,并通过提供工具和具有很强扩展性的浏览器使人们创造出神奇的东西.使web开发者使用Firefox的时候,浏览器可以提供大量开发工具和选 ...

  7. CSS用Id选择器在本页写样式

    <title>静夜思</title><style type="text/css">#red{color:#ff0000; font-weight ...

  8. 26.打印所有和为S的连续正整数序列[FindContinuousSequencesWithSumS]

    [题目] 输入一个正数n,输出所有和为n连续正数序列.例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5.4-6和7-8. [分析] 这是网易的一道面试题.这道 ...

  9. linux(Ubuntu)安装QQ2013

    首先简述自己的系统配置:win7+ ubuntu12.04 linuxQQ 有各种版本,这里介绍两种:linuxQQ 和 wineQQ 1 ------linuxqq是QQ简化版,功能很少,界面很差, ...

  10. [SVN(Ubuntu)] SVN 查看历史详细信息

    转载: http://lee2013.iteye.com/blog/1074457 以下内容,对ubuntu命令行查看代码变化非常有用. SVN 查看历史信息 通过svn命令可以根据时间或修订号去除过 ...