結果

問題 No.3143 Colorless Green Parentheses Sleep Furiously
ユーザー titia
提出日時 2025-05-17 02:45:39
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 173 ms / 2,000 ms
コード長 940 bytes
コンパイル時間 378 ms
コンパイル使用メモリ 12,288 KB
実行使用メモリ 16,512 KB
最終ジャッジ日時 2025-05-17 02:45:46
合計ジャッジ時間 6,679 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,K=map(int,input().split())
S=input().strip()

flag=1

now=0
divideflag=0

for s in S:
    if s=="(":
        now+=1
    else:
        now-=1

    if now<0:
        flag=0

    if now==0:
        divideflag+=1

if now!=0 or flag==0:
    print("No")
    exit()


LIST=[-1]*len(S)
Q=[]
for i in range(len(S)):
    if S[i]=="(":
        Q.append(i)
    else:
        x=Q.pop()

        LIST[i]=x
        LIST[x]=i



ANS=[]
for i in range(len(S)):
    s=S[i]
    if ANS and ANS[-1]==")" and s=="(":
        ANS.append("+")
    if ANS and ANS[-1]=="(" and s==")":
        ANS.append("1+1")
    if ANS and ANS[-1]=="(" and s=="(":
        to=LIST[i]

        if to+1<len(S) and S[to+1]==")":
            ANS.append("1+")

    ANS.append(s)

A="".join(ANS)
c=A.count("1")

if c>K:
    print("No")
elif divideflag==1 and c==K:
    print("No")
else:
    A+="+1"*(K-c)

    print("Yes")
    print(A)
        
0