UVA
来源:互联网 发布:中国阶层固化 知乎 编辑:程序博客网 时间:2024/06/03 14:40
//利用栈来解析表达式 //本题表达式比较简单,可以用一个栈来完成;遇到字母时入栈,遇到右括号时出栈并计算,然后结果入栈。因为输入保证合法,括号无须入栈 #include <iostream>#include <string>#include <stack>#include <cctype>using namespace std;struct Martrix{int a, b;Martrix(int a = 0, int b = 0):a(a), b(b){}} m[26];stack<Martrix>s;int main(){int n;cin >> n;for (int i = 0; i < n; i++){string name;cin >> name;int k = name[0] - 'A';cin >> m[k].a >> m[k].b;}string expr;while (cin >> expr){int len = expr.length();int error = 0, ans = 0;for (int i = 0; i < len; i++){char &tp = expr[i]; //tp is short for temp if (isalpha(tp)) s.push(m[tp - 'A']);else if (tp == ')'){Martrix m2 = s.top(); s.pop();Martrix m1 = s.top(); s.pop();if (m1.b != m2.a){error = 1; break;}ans += m1.a * m1.b * m2.b;s.push(Martrix(m1.a, m2.b));}}if (error) cout << "error" << endl;else cout << ans << endl;}return 0;}
阅读全文
0 0
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- 2017/9/23周测(CF2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 2))
- UVA
- HDU 5699 货物运输 ( 二分 + 不等式判断 )
- Java9 中的Jshell 操作
- Qt 学习笔记--------------------使用纯代码写dialog 与 UI文件
- UVA
- Python客户端注册到服务中心Eureka
- mysql学习总结(三)
- Python Challenge
- bzoj1600 [Usaco2008 Oct]建造栅栏
- tomcat部署项目启动报错
- UVA
- UVA
- <c语言经典100例>c19 猴子吃桃