結果

問題 No.708 (+ー)の式
ユーザー Khiromu
提出日時 2018-06-29 23:32:50
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 3,482 bytes
コンパイル時間 683 ms
コンパイル使用メモリ 71,016 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-01 00:17:59
合計ジャッジ時間 1,276 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 5 WA * 7
権限があれば一括ダウンロードができます

ソースコード

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