結果

問題 No.708 (+ー)の式
ユーザー RTIA1227RTIA1227
提出日時 2018-06-29 23:16:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,208 bytes
コンパイル時間 642 ms
コンパイル使用メモリ 82,336 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-13 15:39:16
合計ジャッジ時間 1,401 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#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 int

LL 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;
}
0