poj-1686-Lazy Math Instructor(stack)
来源:互联网 发布:天干地支年算法 编辑:程序博客网 时间:2024/05/17 07:00
题目地址
http://poj.org/problem?id=1686
Code
#include <stdio.h>#include <iostream>#include <stdlib.h>#include <string.h>#include <queue>#include <stack>#include <string>#include <map>#include <sstream>#include <vector>#include <math.h>#include <algorithm>#define INF 0x3fffffff#define N 1005#define p(x) cout << (x) << endl;int dx[4] = {0, 0, 1, -1};int dy[4] = {1, -1, 0, 0};using namespace std;typedef long long LL;int a[N];int init[N];int C(char a, char b){ int r ; switch(b){ case '+': case '-': if(a=='(' || a=='#') r = -1 ; //入栈 else r = 1 ; //计算 break ; case '*': case '/': if(a=='*' || a=='/' || a==')') r =1 ; //计算 else r = -1 ; //入栈 break ; case '(': if(a == ')'){ printf("error!\n"); exit(1) ; } else{ r = -1 ; //入栈 } break ; case ')': if(a == '(') r = 0 ; else r = 1 ; break ; case '#': if(a=='#') r = 0 ; else r = 1 ; break ; } return r ;}int cal(int a, int b , char c){ int res ; switch(c){ case '+': res = a + b ; break ; case '-': res = a - b ; break ; case '*': res = a * b ; break ; case '/': res = a / b ; break ; } //printf("%d = %d %c %d\n", res, a, c, b); return res ;}int hand(string s) { map<char, int> mp; for (int i = 0; i <= 9; i++) { mp['0'+i] = i; } int v = 1; for (int i = 0; i < s.size(); i++) { if (s[i] == ' ' || s[i] == '\t') { continue; } if (s[i] == '(' || s[i] == ')' || s[i] == '+' || s[i] == '-' || s[i] == '*') { continue; } if ('0' <= s[i] && s[i] <= '9') { continue; } // not same with 0~9 if (mp[s[i]] == 0) { mp[s[i]] = s[i] - 'A' + 10; } } //p(mp['a']) //p(mp['b']) //p(mp['c']) //p(s) stack<char> f; f.push('#'); stack<int> d; int x, y; for (int i = 0; i < s.size(); i++) { if (s[i] == ' ' || s[i] == '\t') { continue; } char pre = f.top(); char c = s[i]; switch (c) { case '(' : case ')' : case '+' : case '-' : case '*' : case '#' : switch(C(pre, c)){ case 0: f.pop(); break ; case -1 : f.push(c); break ; case 1 : i--; f.pop(); int a = d.top(); d.pop(); int b = d.top(); d.pop(); d.push(cal(b, a, pre)); break ; } break; default : d.push(mp[c]); break; } } return d.top();}int main() {#ifndef ONLINE_JUDGE freopen("in", "r", stdin);#else //#endif int n; cin >> n; getchar(); for (int i = 0; i < n; i++) { string s1, s2; getline(cin, s1); getline(cin, s2); s1 += "#"; s2 += "#"; int ans1 = hand(s1); int ans2 = hand(s2); //cout << ans1 << " " << ans2 << endl; if (ans1 == ans2) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0;}
参考
- http://blog.csdn.net/ivan_zjj/article/details/7354271
阅读全文
0 0
- poj-1686-Lazy Math Instructor(stack)
- 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 栈的应用
- POJ 1686 Lazy Math Instructor (中缀表达式计算)
- POJ 1686 Lazy Math Instructor(数据结构-表达式树)
- E - Lazy Math Instructor
- Lazy Math Instructor
- POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑
- Lazy Math Instructor - POJ 1686 栈+提比的强行AC技能
- ZOJ 1418 Lazy Math Instructor
- HDU 1686 Lazy Math Instructor(中缀转后缀,栈模拟)
- poj1686Lazy Math Instructor
- Installing TensorFlow from Sources
- Ubuntu16.04 Wiz绿色版 ibus = 中文输入
- javase回顾一
- Install Bazel on Ubuntu
- nrf开发资源搜集
- poj-1686-Lazy Math Instructor(stack)
- Android Studio基础技巧篇
- (五)懒加载模块
- Mac上Homebrew的使用 (Homebrew 使 OS X 更完整)
- Java环境配置
- Koa2 学习笔记(第五天)
- CodeForces Sorting by Subsequences
- Jquey Deferred 独立实现版本不依赖jquery
- (六)懒加载和依赖注入树