結果

問題 No.3143 Colorless Green Parentheses Sleep Furiously
ユーザー GOTKAKO
提出日時 2025-05-16 23:15:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 1,317 bytes
コンパイル時間 1,758 ms
コンパイル使用メモリ 201,208 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-05-17 00:35:47
合計ジャッジ時間 3,851 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int N,K; cin >> N >> K;
    string s; cin >> s;
    assert(s.size() == N); 
    int sum = 0,open = 0,back = 0,count = 0;
    stack<int> St; St.push(0);
    for(auto c : s){
        if(open == 0) count++;
        if(c == '(') St.top()++,open++,back = 0,St.push(0);
        else{
            sum += max(0,2-St.top()); St.pop();
            open--,back = 1;
        }    
        if(open < 0){cout << "No\n"; return 0;}
    }
    if(count == 1 && K == sum){cout << "No\n"; return 0;} //え
    if(open > 0 || sum > K){cout << "No\n"; return 0;}
    cout << "Yes\n";
    bool first = true;
    back = -1; St.top() = 0;
    string answer = "";
    for(auto c : s){
        if(c == '('){
            if(back == 1 && !first) answer += '+';
            answer += '(';
            back = 0; open++; St.top()++,St.push(0);
        }
        else{
            if(St.top() == 0) answer += "1+1)";
            else if(St.top() == 1) answer += "+1)";
            else answer += ")";
            back = 1; open--; St.pop();
        }
        first = false;
    }
    int more = K-sum;
    while(more--) answer += "+1";
    for(auto c : answer) K -= c=='1';
    assert(K == 0);
    cout << answer << endl;
}
0