結果

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

ソースコード

diff #

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

int main(){
    int N,K;
    string S;
    cin >> N >> K;
    cin >> S;

    vector<char> stack;
    for (int i=0; i<N; i++){
        if (stack.size()>0 && stack.back()=='(' && S[i]==')'){
            stack.pop_back();
        } else {
            stack.push_back(S[i]);
        }
    }

    if (!stack.empty()){
        cout << "No" << endl;
    } else {
        int cnt=0;
        for (int i=0; i<N-1; i++){
            if (S[i]=='(' && S[i+1]==')'){
                cnt++;
            }
        }
        if (cnt*2+(N/2-cnt)<=K){
            vector<char> ans;
            for (int i=0; i<N-1; i++){
                if (S[i]=='(' && S[i+1]==')'){
                    ans.push_back('(');
                    ans.push_back('1');
                    ans.push_back('+');
                    ans.push_back('1');
                    K -= 2;
                } else if (S[i]==')' && S[i+1]=='('){
                    ans.push_back(S[i]);
                    ans.push_back('+');
                } else if (S[i]=='('){
                    ans.push_back(S[i]);
                } else {
                    ans.push_back(S[i]);
                    ans.push_back('+');
                    ans.push_back('1');
                    K -= 1;
                }
            }
            ans.push_back(S.back());
            for (int i=0; i<K; i++){
                ans.push_back('+');
                ans.push_back('1');
            }
            cout << "Yes" << endl;
            for (int i = 0; i < (int)ans.size(); i++) {
                cout << ans[i];
            }
            cout << endl;
        } else {
            cout << "No" << endl;
        }
    }
    return 0;
}
0