結果

問題 No.22 括弧の対応
ユーザー yn
提出日時 2015-08-03 16:31:39
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 34 ms / 5,000 ms
コード長 763 bytes
コンパイル時間 86 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-07-20 07:03:11
合計ジャッジ時間 1,461 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

N, K = map(int, input().split())
S = input()
lcount = 0
rcount = 0
judge = 0

if S[K-1] == '(':
    for i in range(K):
        if S[i] == '(':
            lcount += 1
        else:
            lcount -= 1
    judge = lcount - 1

    i += 1
    while lcount != judge :
        if i >= N:
            break
        if S[i] == ')':
            lcount -= 1
        else:
            lcount += 1
        i += 1
    print(i)

else:
    for i in range(N-1, K-2, -1):
        if S[i] == ')':
            rcount += 1
        else:
            rcount -= 1

    judge = rcount - 1
    i -= 1

    while rcount != judge :
        if i < 0 :
            break
        if S[i] == '(':
            rcount -= 1
        else:
            rcount += 1
        i -= 1

    print(i+2)
0