結果

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

ソースコード

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;
    stack<int> St; St.push(0);
    for(auto c : s){
        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(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