stack UVA 442 Matrix Chain Multiplication
题意:给出每个矩阵的行列,计算矩阵的表达式,如果错误输出error,否则输出答案
分析:表达式求值,stack 容器的应用:矩阵的表达式求值A 矩阵是a * b,B 矩阵是b * c,则A * B 是a * c。遇到')'弹出两个矩阵相乘,错误的话直接break
收获:以前做过了,现在会表达式求值后,这题也太容易了
代码:
- /************************************************
- * Author :Running_Time
- * Created Time :2015-8-29 10:22:51
- * File Name :UVA_442.cpp
- ************************************************/
- #include <cstdio>
- #include <algorithm>
- #include <iostream>
- #include <sstream>
- #include <cstring>
- #include <cmath>
- #include <string>
- #include <vector>
- #include <queue>
- #include <deque>
- #include <stack>
- #include <list>
- #include <map>
- #include <set>
- #include <bitset>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- #define lson l, mid, rt << 1
- #define rson mid + 1, r, rt << 1 | 1
- typedef long long ll;
- const int N = 1e5 + 10;
- const int INF = 0x3f3f3f3f;
- const int MOD = 1e9 + 7;
- struct Martrix {
- int a, b;
- Martrix (int _a = 0, int _b = 0) : a (_a), b (_b) {};
- }m[26];
- int main(void) {
- int n;
- string name;
- cin >> n;
- for (int i=1; i<=n; ++i) {
- cin >> name; int k = name[0] - 'A';
- cin >> m[k].a >> m[k].b;
- }
- string exp;
- stack<Martrix> S;
- while (cin >> exp) {
- bool error = false;
- int ans = 0;
- int len = exp.length ();
- for (int i=0; i<len; ++i) {
- if ('A' <= exp[i] && exp[i] <= 'Z') S.push (m[exp[i]-'A']);
- else if (exp[i] == ')') {
- Martrix m1 = S.top (); S.pop ();
- Martrix m2 = S.top (); S.pop ();
- if (m2.b != m1.a) {
- error = true; break;
- }
- ans += m2.a * m2.b * m1.b;
- S.push (Martrix (m2.a, m1.b));
- }
- }
- if (error) cout << "error" << endl;
- else cout << ans << endl;
- }
- return 0;
- }
stack UVA 442 Matrix Chain Multiplication的更多相关文章
- 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 ...
- UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)
意甲冠军 由于矩阵乘法计算链表达的数量,需要的计算 后的电流等于行的矩阵的矩阵的列数 他们乘足够的人才 非法输出error 输入是严格合法的 即使仅仅有两个相乘也会用括号括起来 并且括号中 ...
- UVa 442 Matrix Chain Multiplication(栈的应用)
题目链接: https://cn.vjudge.net/problem/UVA-442 /* 问题 输入有括号表示优先级的矩阵链乘式子,计算该式进行的乘法次数之和 解题思路 栈的应用,直接忽视左括号, ...
- UVA - 442 Matrix Chain Multiplication(栈模拟水题+专治自闭)
题目: 给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数. 思路: 遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的 ...
- 例题6-3 Matrix Chain Multiplication ,Uva 442
这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试 得到的经验有以下几点: 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习 有时候代码不对,得到的结果却是对的(之后总结以下常见 ...
- UVA 442 二十 Matrix Chain Multiplication
Matrix Chain Multiplication Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- UVa442 Matrix Chain Multiplication
// UVa442 Matrix Chain Multiplication // 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.假定A和m*n的,B是n*p的,那么AB是m*p的,乘法 ...
- Matrix Chain Multiplication(表达式求值用栈操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...
- ACM学习历程——UVA442 Matrix Chain Multiplication(栈)
Description Matrix Chain Multiplication Matrix Chain Multiplication Suppose you have to evaluate ...
随机推荐
- App架构设计经验谈:服务端接口的设计
App与服务器的通信接口如何设计得好,需要考虑的地方挺多的,在此根据我的一些经验做一些总结分享,旨在抛砖引玉. 安全机制的设计 现在,大部分App的接口都采用RESTful架构,RESTFul最重要的 ...
- Unity3D模型的细致纹理问题解决办法
http://hunterwang.diandian.com/post/2012-09-28/40039798509 也许有人也遇到过同样的问题,也许解决方式不同,我来介绍一下偶尔尝试发现的解决办法. ...
- compact过滤数组中的nil
http://ruby-doc.org/core-2.2.0/Array.html#method-i-compact compact → new_aryclick to toggle source R ...
- 第3章 K近邻法
参考: http://www.cnblogs.com/juefan/p/3807713.html http://blog.csdn.net/v_july_v/article/details/82036 ...
- 不用任何图片,只用简单的css写出唯美的钟表,就问你行吗?
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAGHCAIAAABJR31QAAAgAElEQVR4nOy9aXhc1ZUurPvcH7f73n ...
- Firefox上Web开发工具库一览
Firefox的目标之一就是尽可能地使web开发者的生活更简单高效,并通过提供工具和具有很强扩展性的浏览器使人们创造出神奇的东西.使web开发者使用Firefox的时候,浏览器可以提供大量开发工具和选 ...
- CSS用Id选择器在本页写样式
<title>静夜思</title><style type="text/css">#red{color:#ff0000; font-weight ...
- 26.打印所有和为S的连续正整数序列[FindContinuousSequencesWithSumS]
[题目] 输入一个正数n,输出所有和为n连续正数序列.例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5.4-6和7-8. [分析] 这是网易的一道面试题.这道 ...
- linux(Ubuntu)安装QQ2013
首先简述自己的系统配置:win7+ ubuntu12.04 linuxQQ 有各种版本,这里介绍两种:linuxQQ 和 wineQQ 1 ------linuxqq是QQ简化版,功能很少,界面很差, ...
- [SVN(Ubuntu)] SVN 查看历史详细信息
转载: http://lee2013.iteye.com/blog/1074457 以下内容,对ubuntu命令行查看代码变化非常有用. SVN 查看历史信息 通过svn命令可以根据时间或修订号去除过 ...