結果

問題 No.22 括弧の対応
ユーザー LfSYEy1US1RWVNdLfSYEy1US1RWVNd
提出日時 2021-03-22 21:28:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 675 bytes
コンパイル時間 217 ms
コンパイル使用メモリ 10,968 KB
実行使用メモリ 7,916 KB
最終ジャッジ日時 2023-08-16 00:05:47
合計ジャッジ時間 1,697 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,788 KB
testcase_01 AC 16 ms
7,872 KB
testcase_02 AC 17 ms
7,876 KB
testcase_03 AC 16 ms
7,828 KB
testcase_04 AC 15 ms
7,820 KB
testcase_05 AC 15 ms
7,796 KB
testcase_06 AC 15 ms
7,864 KB
testcase_07 AC 16 ms
7,876 KB
testcase_08 AC 15 ms
7,816 KB
testcase_09 AC 16 ms
7,828 KB
testcase_10 AC 15 ms
7,824 KB
testcase_11 AC 15 ms
7,876 KB
testcase_12 AC 15 ms
7,792 KB
testcase_13 AC 15 ms
7,864 KB
testcase_14 AC 15 ms
7,788 KB
testcase_15 AC 15 ms
7,916 KB
testcase_16 AC 15 ms
7,828 KB
testcase_17 AC 15 ms
7,916 KB
testcase_18 AC 15 ms
7,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def tansaku(input_line,K,N):   #指定の位置から"("で+1、")"で-1を足して0になるのが対応した位置
	if input_line[K-1] == ")":   #右に探すので指定の位置が)のとき逆順に
		input_line = input_line[::-1]
		K = N - K + 1
	banngouduke = []
	if input_line[K-1] == ")":
		num = -1 
	else:
		num = 1 
	count = K
	for i in input_line[K:]:
		if num != 0:
			if i == "(":
				num += 1
				count += 1
			else:
				num -= 1
				count +=1
			if num == 0:	
				if input_line[K-1] == ")":
					return N - count + 1 
					break
				else:
					return count
					break
N,K = map(int,input().split(" "))
input_line = input()
print(tansaku(input_line,K,N))


0