結果
問題 | No.708 (+ー)の式 |
ユーザー |
![]() |
提出日時 | 2018-06-29 23:16:43 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,208 bytes |
コンパイル時間 | 872 ms |
コンパイル使用メモリ | 83,696 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 00:14:14 |
合計ジャッジ時間 | 1,227 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#include<iostream>#include<vector>#include<algorithm>#include<functional>#include<queue>#include<stack>#include<set>#include<climits>#include<cstdlib>#include<cmath>#include<string>using namespace std;#define INF 1 << 29#define LL long long intLL const MOD = 1000000007;LL priority(char x,char y){if((x == '+' || x == '-') && y == '+'){return 1;}if((x == '+' || x == '-') && y == '-'){return 1;}if((x == '+' || x == '-') && y == '('){return -1;}if((x == '+' || x == '-') && y == ')'){return 1;}if((x == '+' || x == '-') && y == 'E'){return 1;}if(x == '(' && y == ')'){return 0;}if(x == '('){return -1;}if(x == 'S' && x == 'E'){return 0;}return -1;}int main(){cin.tie(0);ios::sync_with_stdio(false);string str;cin >> str;str += 'E';stack<char> st;st.push('S');int i = 0;string gyakupo = "";bool endFrag = true;while(i < str.size() && endFrag){int tmp = str[i] - '0';if(tmp >= 0 && tmp <= 9){gyakupo += str[i];i++;}else{int ret = priority(st.top(),str[i]);if(ret > 0){gyakupo += st.top();st.pop();}else if(ret < 0){st.push(str[i]);i++;}else{if(st.top() == '('){st.pop();i++;}else{endFrag = false;}}}}stack<LL> sim;for(int j = 0; j < gyakupo.size(); j++){LL t = gyakupo[j] - '0';if(t >= 0 && t <= 9){sim.push(t);}else{if(gyakupo[j] == '+'){LL sec = sim.top();sim.pop();LL fis = sim.top();sim.pop();sim.push(fis+sec);}else{LL sec = sim.top();sim.pop();LL fis = sim.top();sim.pop();sim.push(fis-sec);}}}cout << sim.top() << endl;return 0;}