HDU 1686 Lazy Math Instructor(中缀转后缀,栈模拟)
来源:互联网 发布:网络专科学位证有用吗 编辑:程序博客网 时间:2024/05/17 07:23
HDU 1686 Lazy Math Instructor(中缀转后缀,栈模拟)
ACM
题目地址:POJ 1686 Lazy Math Instructor
题意:
问两个等式是否相等。
分析:
栈模拟把中缀转后缀,然后用后缀算出值,再进行比较。
直接把字母看成ASCII值就行了。
代码:
/** Author: illuz <iilluzen[at]gmail.com>* Blog: http://blog.csdn.net/hcbbt* File: 1686.cpp* Create Date: 2014-08-04 20:05:00* Descripton: simulate, expression */#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <stack>using namespace std;const int N = 90;int prior(char c) { if(c == '(') return 0; else if (c == '*') return 2; else return 1;}void convert(char* s, char* tmp) {int len = strlen(s), n = 0;int res, rhs, lhs;stack<int> st;for (int i = 0; i < len; i++) {char r = s[i];if (r == ' ')continue;if ((r >= 'a' && r <= 'z') || (r >= '0' && r <= '9'))tmp[n++] = r;else {if (st.empty() || r == '(')st.push(r);else if (r == ')') {while (!st.empty() && st.top() != '(') {tmp[n++] = st.top();st.pop();}st.pop();} else {while (!st.empty() && prior(r) <= prior(st.top())) {tmp[n++] = st.top();st.pop();}st.push(r);}}}while (!st.empty()) {tmp[n++] = st.top();st.pop();}tmp[n] = 0;}int calc(char* t) {int len = strlen(t);int rhs = 0, lhs = 0;stack<int> st;for (int i = 0; i < len; i++) {if (t[i] >= '0' && t[i] <= '9') st.push(t[i] - '0');else if (t[i] <= 'z' && t[i] >= 'a')st.push(t[i]);else {rhs = st.top();st.pop();lhs = st.top();st.pop();if (t[i] == '+')st.push(rhs + lhs);else if (t[i] == '-')st.push(lhs - rhs);elsest.push(rhs * lhs);}}return st.top();}int main() {char str[N], temp[N];int n;scanf("%d\n",&n);while (n--) {gets(str);convert(str, temp);int ans1 = calc(temp);gets(str);convert(str, temp);int ans2 = calc(temp);if (ans1 == ans2)printf("YES\n");elseprintf("NO\n");}}
0 0
- HDU 1686 Lazy Math Instructor(中缀转后缀,栈模拟)
- POJ 1686 Lazy Math Instructor (中缀表达式计算)
- poj 1686 Lazy Math Instructor (字符串)
- POJ 1686 Lazy Math Instructor
- POJ 1686 Lazy Math Instructor
- POJ 1686 Lazy Math Instructor 栈的应用
- zoj 1418 || poj 1686 Lazy Math Instructor(等价表达式)
- poj 1686 Lazy Math Instructor(表达式求值)
- zoj 1418||poj 1686 Lazy Math Instructor
- POJ 1686 Lazy Math Instructor 笔记
- poj-1686-Lazy Math Instructor(stack)
- E - Lazy Math Instructor
- Lazy Math Instructor
- ZOJ 1418 Lazy Math Instructor
- POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑
- Lazy Math Instructor - POJ 1686 栈+提比的强行AC技能
- POJ 1686 Lazy Math Instructor(数据结构-表达式树)
- 中缀转后缀 栈
- 求条形图中最大矩形的面积的两种算法
- Spring MVC和Struts2的比较的优点
- 猜想 QQ好友分组 (微信好友)多层次分组策略 QQ好友过多
- HDU 1166 敌兵布阵(线段树入门,单点更新)
- [笔记]javascript面向对象 - 继承的几种模式
- HDU 1686 Lazy Math Instructor(中缀转后缀,栈模拟)
- JavaSe基础XX04——多维数组
- HDU 3264 Balanced Lineup(线段树,最值查询)
- 布线问题
- 输出路径的最小编辑距离
- 安慰奶牛
- 译稿:UserAgent的历史变迁
- 15.2 Palindrome Number
- 浏览器userAgent大全