結果

問題 No.3021 Maximize eval
ユーザー bal4u
提出日時 2025-02-16 19:02:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 1,296 bytes
コンパイル時間 2,299 ms
コンパイル使用メモリ 193,004 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2025-02-16 19:02:42
合計ジャッジ時間 4,043 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int T; 
    cin >> T;
    while(T--){
        string S;
        cin >> S;
        int n = S.size();
        string ans;
        ans.reserve(n);
 
        int curSign = +1;  
 
        for (int i = 0; i < n; i++){
            char c = S[i];
            if(c != '?'){
                ans.push_back(c);
                if(c == '+' || c == '-'){
                    curSign = (c == '+' ? +1 : -1);
                }
            } else {
                bool atTermStart = (ans.empty() || (ans.back()=='+' || ans.back()=='-'));
                if(atTermStart){
                    char dig = (curSign == 1 ? '9' : '1');
                    ans.push_back(dig);
                } else {
                    if(curSign == 1){
                        ans.push_back('9');
                    } else {
                        if(i < n-1 && S[i+1] != '+' && S[i+1] != '-'){
                            ans.push_back('+');
                            curSign = 1;
                        } else {
                            ans.push_back('1');
                        }
                    }
                }
            }
        }
 
        cout << ans << "\n";
    }
    return 0;
}
0