結果

問題 No.22 括弧の対応
ユーザー rihitorihito
提出日時 2018-09-10 13:51:29
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 1,081 bytes
コンパイル時間 470 ms
コンパイル使用メモリ 10,480 KB
実行使用メモリ 8,188 KB
最終ジャッジ日時 2023-09-27 13:30:41
合計ジャッジ時間 1,298 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,760 KB
testcase_01 AC 16 ms
7,824 KB
testcase_02 AC 16 ms
7,760 KB
testcase_03 AC 17 ms
8,164 KB
testcase_04 AC 16 ms
7,772 KB
testcase_05 AC 17 ms
7,768 KB
testcase_06 AC 16 ms
7,892 KB
testcase_07 AC 16 ms
8,188 KB
testcase_08 AC 15 ms
7,756 KB
testcase_09 AC 16 ms
7,784 KB
testcase_10 AC 16 ms
7,844 KB
testcase_11 AC 16 ms
7,744 KB
testcase_12 AC 15 ms
7,776 KB
testcase_13 AC 15 ms
8,172 KB
testcase_14 AC 15 ms
7,780 KB
testcase_15 AC 16 ms
7,872 KB
testcase_16 AC 15 ms
7,920 KB
testcase_17 AC 15 ms
7,780 KB
testcase_18 AC 15 ms
7,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

S = list(input())

L = K - 1
# 例:サンプル3)K=5番目の場合は、0から数えると、4番目になるため。

p = 1 if S[L] == "(" else -1
# 例)K番目の文字が「(」ならば、p = 1、4番目の文字が「)」ならば、p = -1。
# これは「(」ならば、→方向を、「)」ならば、←方向を数える。
# 例:サンプル3)5番目の文字は「(」なので、→方向を数えていく。

count = 0
while(0 <= L):
    if S[L] == '(':
        count += p
    else:
        count -= p
# 例)→の時は「(」ならばcountは1加算。「)」ならばcountは1減少。countは0以下でループは停止し、Lにはこの間の操作を繰り返した数を合計する。
# ←の時はその逆。
# 例:サンプル3)ループでそれぞれ、count = 1,2,3,2,1,0(終了)   L=4+1,6,7,8,9 という値をとる。
    if count <= 0:
        break
    L += p

print(L + 1)
# 最後に0からではなく、1から数えるために+1。
# 例:サンプル3)L + 1 =9 + 1 =10番目
0