結果
| 問題 |
No.708 (+ー)の式
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-06-29 23:08:54 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,134 bytes |
| コンパイル時間 | 1,566 ms |
| コンパイル使用メモリ | 172,672 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-01 00:11:43 |
| 合計ジャッジ時間 | 2,461 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 10 RE * 2 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int main(){
string s; cin >> s;
stack<int> op, optmp; bool braket = false;
int ans = -1e5; int tmp = -1e5;
for (int i = 0; i < (int) s.size(); i++) {
if (s[i] == '+') {
if (!braket) op.push(1);
else optmp.push(1);
}
else if (s[i] == '-') {
if (!braket) op.push(-1);
else optmp.push(-1);
}
else if (s[i] == '(') {
braket = true;
} else if (s[i] == ')') {
braket = false;
ans += op.top() * tmp;
tmp = -1e5; op.pop();
} else {
int n = s[i] - '0';
if (braket) {
if (tmp == -1e5) {
tmp = n;
if (!optmp.empty()) {
tmp *= optmp.top();
optmp.pop();
}
}
else {
tmp += optmp.top() * n;
optmp.pop();
}
} else {
if (ans == -1e5) {
ans = n;
if (!op.empty()) {
ans *= op.top();
op.pop();
}
}
else {
ans += op.top() * n;
op.pop();
}
}
}
}
cout << ans << endl;
return 0;
}