結果

問題 No.22 括弧の対応
ユーザー nishimanishima
提出日時 2015-10-12 01:22:50
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 422 bytes
コンパイル時間 58 ms
コンパイル使用メモリ 11,372 KB
実行使用メモリ 15,960 KB
最終ジャッジ日時 2023-09-28 12:29:20
合計ジャッジ時間 3,040 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,220 KB
testcase_01 AC 83 ms
15,096 KB
testcase_02 AC 83 ms
15,016 KB
testcase_03 AC 87 ms
15,960 KB
testcase_04 AC 85 ms
15,036 KB
testcase_05 AC 85 ms
15,296 KB
testcase_06 AC 87 ms
15,232 KB
testcase_07 WA -
testcase_08 AC 86 ms
15,420 KB
testcase_09 WA -
testcase_10 AC 86 ms
15,528 KB
testcase_11 AC 86 ms
15,156 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 86 ms
15,840 KB
testcase_16 AC 85 ms
15,732 KB
testcase_17 AC 85 ms
15,156 KB
testcase_18 AC 83 ms
15,148 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

d1 =  $stdin.gets.chomp.split(/ /).map(&:to_i)
d2 =  $stdin.gets.chomp.split(//).map{ |e| 
	if e == '(' then 1
	else		    -1
	end
}
n = d1[ 0 ]
k = d1[ 1 ]
ans = 0

if d2[ k - 1 ] == 1	# '('
	sum = d2[ k - 1 ]
	k.upto( n - 1 ){ |i|
		sum += d2[ i ]
		if sum == 0
			ans = i + 1
			break
		end
	}
else				# ')'
	sum = d2[ k - 1 ]
	(k-2).downto( 0 ){ |i|
		sum += d2[ i ]
		if sum == 0
			ans = i + 1
		end
	}

end
puts ans
0