矩阵连乘 LRJ白书 p141 栈 解析表达式
2017-04-20 10:19
260 查看
#include<iostream> #include<cstdio> #include<cstring> #include<sstream> #include<algorithm> #include<queue> #include<vector> #include<cmath> #include<map> #include<stack> #include<set> #include<fstream> #include<memory> #include<string> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define MAXN 102 #define INF 1000000009 /* 利用栈解析表达式 */ struct Matrix { int a, b; Matrix(int _a = 0,int _b=0):a(_a),b(_b){} }m[26]; stack<Matrix> s; int main() { int n; scanf("%d", &n); string str; for (int i = 0; i < n; i++) { cin >> str; int k = str[0] - 'A'; cin >> m[k].a >> m[k].b; } string expr; while (cin >> expr) { int len = expr.length(); bool error = false; int ans = 0; for (int i = 0; i < len; i++) { if (isalpha(expr[i])) s.push(m[expr[i] - 'A']); else if (expr[i] == ')') { Matrix m2 = s.top(); s.pop(); Matrix m1 = s.top(); s.pop(); if (m1.b != m2.a) { error = true; break; } ans += m1.a*m1.b*m2.b; s.push(Matrix(m1.a, m2.b)); } } if (error) cout << "error\n"; else cout << ans << endl; } }
相关文章推荐
- 矩阵连乘 和表达式加括号求最大值
- C++11 lambda 表达式解析
- 动态规划——矩阵连乘的问题
- JavaScript中的正则表达式解析
- 矩阵连乘算法思想
- 矩阵连乘 转自matrix67大牛
- 列表解析和矩阵
- 对lambda表达式的解析(一)
- js正则表达式之解析——URL的正则表达式
- C++11 lambda 表达式解析
- 动态规划-矩阵连乘问题
- 2013-正则表达式解析文本
- 深入浅出解析正则表达式-替换原则
- PYTHON解析中文的正则表达式
- java正则表达式解析html示例分享
- 润乾报表性能管理之表达式预解析
- 【动态规划】矩阵连乘问题
- HDU 1588 二分矩阵连乘求和
- pugixml 一个xml解析库 简单快速, 支持XPath表达式, 感谢作者
- 矩阵连乘问题