結果

問題 No.22 括弧の対応
コンテスト
ユーザー yn
提出日時 2015-08-03 16:31:39
言語 Python3
(3.15.2 + numpy 2.5.4 + scipy 1.18.1 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 18 ms / 5,000 ms
+ 238µs
コード長 763 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 61 ms
コンパイル使用メモリ 14,720 KB
実行使用メモリ 10,240 KB
最終ジャッジ日時 2026-11-10 18:46:33
合計ジャッジ時間 2,426 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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