結果

問題 No.708 (+ー)の式
ユーザー KhiromuKhiromu
提出日時 2018-06-29 23:32:50
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,482 bytes
コンパイル時間 584 ms
コンパイル使用メモリ 70,916 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 15:42:47
合計ジャッジ時間 1,390 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>

using namespace std;
#define CK(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second
typedef long long ll;

const int INF = 1e9 + 7;
const long long LLINF = 1e18;

string s;
int main() {
    cin>>s;

    stack<int> stk;
    REP(i, 0, s.size()){
        //if(!stk.empty()) cout<<"stk:"<<i<<" "<<stk.top()<<endl;
        if(s[i]>='0' && s[i]<='9'){
            stk.push(s[i]-'0');
        }
        else if(s[i]=='+' && s[i+1]!='('){
            int x=stk.top();
            stk.pop();
            x+=(s[i+1]-'0');
            stk.push(x);
            i++;
        }
        else if(s[i]=='-' && s[i+1]!='('){
            int x=stk.top();
            stk.pop();
            x-=(s[i+1]-'0');
            stk.push(x);
            i++;
        }
        else if(s[i]=='+' && s[i+1]=='('){
            int j=i+2;
            stack<int> stk2;
            while(s[j]!=')'){
                if(s[j]>='0' && s[j]<='9'){
                    stk2.push(s[j]-'0');
                }
                else if(s[j]=='+'){
                    int x=stk2.top();
                    stk2.pop();
                    x+=(s[j+1]-'0');
                    stk2.push(x);
                    j++;
                }
                else if(s[j]=='-'){
                    int x=stk2.top();
                    stk2.pop();
                    x-=(s[j+1]-'0');
                    stk2.push(x);
                    j++;
                }
                j++;
            }
            int x=stk.top(), y=stk2.top();
            stk.pop();
            x+=y;
            stk.push(x);
            i=j+1;
        }
        else if(s[i]=='-' && s[i+1]=='('){
            int j=i+2;
            stack<int> stk2;
            while(s[j]!=')'){
                if(s[j]>='0' && s[j]<='9'){
                    stk2.push(s[j]-'0');
                }
                else if(s[j]=='+'){
                    int x=stk2.top();
                    stk2.pop();
                    x+=(s[j+1]-'0');
                    stk2.push(x);
                    j++;
                }
                else if(s[j]=='-'){
                    int x=stk2.top();
                    stk2.pop();
                    x-=(s[j+1]-'0');
                    stk2.push(x);
                    j++;
                }
                j++;
            }
            //cout<<"stk2.top()="<<stk2.top()<<endl;
            int x=stk.top(), y=stk2.top();
            stk.pop();
            x+=y;
            stk.push(x);
            i=j+1;
        }
        else if(s[i]=='('){
            int j=i+1;
            stack<int> stk2;
            while(s[j]!=')'){
                if(s[j]>='0' && s[j]<='9'){
                    stk2.push(s[j]-'0');
                }
                else if(s[j]=='+'){
                    int x=stk2.top();
                    stk2.pop();
                    x+=(s[j+1]-'0');
                    stk2.push(x);
                    j++;
                }
                else if(s[j]=='-'){
                    int x=stk2.top();
                    stk2.pop();
                    x-=(s[j+1]-'0');
                    stk2.push(x);
                    j++;
                }
                j++;
            }
            stk.push(stk2.top());
        }
    }
    cout<<stk.top()<<endl;
    return 0;
}
0