結果

問題 No.708 (+ー)の式
ユーザー @abcde@abcde
提出日時 2019-05-25 03:12:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,141 bytes
コンパイル時間 1,634 ms
コンパイル使用メモリ 170,380 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 17:40:07
合計ジャッジ時間 2,291 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){

    // 1. 入力情報取得.
    string S;
    cin >> S;
    
    // 2. 括弧ごとに計算.
    int ans = 0;
    vector<char> v;
    bool isBracket = false;
    char curX, befX = '#';
    for(int i = 0; i < S.size(); i++){
        
        curX = S[i];
        
        // 2-1. 左括弧来たら, フラグ を true に 変更.
        if(curX == '('){
            isBracket = true;
            continue;
        }
        
        // 2-2. 右括弧来たら, フラグ を false に 変更.
        if(curX == ')'){
            isBracket = false;
            int c = 0;
            char bef = '#';
            for(auto &cur : v){
                if(cur == '+' || cur == '-') bef = cur;
                if(cur != '+' && cur != '-'){
                    if(bef == '#') c =  (cur - '0');
                    if(bef == '+') c += (cur - '0');
                    if(bef == '-') c -= (cur - '0');
                }
                // cout << "cur=" << cur << " c=" << c << endl;
            }
            if(befX == '+') ans += c;
            if(befX == '-') ans -= c;
            // cout << "isBracket=" << isBracket << " befX=" << befX << " curX=" << curX << " c=" << c << " ans=" << ans << endl;
            // v を 初期化.
            v = vector<char>();
            continue;
        }
        
        // 2-3. 括弧以外は, 保存 or 計算.
        if(isBracket) v.push_back(curX);
        if(!isBracket && curX != '+' && curX != '-'){
            if(befX == '#') ans =  (curX - '0');
            if(befX == '+') ans += (curX - '0');
            if(befX == '-') ans -= (curX - '0');
            // cout << "isBracket=" << isBracket << " befX=" << befX << " curX=" << curX << " ans=" << ans << endl;
        }
        
        // 2-4. 前回 の '+' or '-' を 保存.
        if((curX == '+' || curX == '-') && !isBracket) befX = curX;

    }

    // 3. 出力 ~ 後処理.
    // ex.
    // 0-(1-2)+(3+4)-5+6-(7+8+9)+0+1+2+3+4+5 = 0
    // 5-(3-2+4)+(3-7-4+1)-5+6-0-(7+2+9)+5+0+1-2+3-4+(5+9)-3+7 = -3
    // のはず.
    cout << ans << endl;
    return 0;
    
}
0