結果

問題 No.22 括弧の対応
ユーザー harhogefooharhogefoo
提出日時 2016-10-05 09:16:47
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,470 ms / 5,000 ms
コード長 348 bytes
コンパイル時間 54 ms
コンパイル使用メモリ 11,260 KB
実行使用メモリ 16,204 KB
最終ジャッジ日時 2023-09-27 13:09:37
合計ジャッジ時間 11,369 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
15,092 KB
testcase_01 AC 72 ms
15,324 KB
testcase_02 AC 73 ms
15,280 KB
testcase_03 AC 1,470 ms
16,204 KB
testcase_04 AC 349 ms
15,708 KB
testcase_05 AC 305 ms
15,888 KB
testcase_06 AC 624 ms
15,792 KB
testcase_07 AC 905 ms
15,968 KB
testcase_08 AC 481 ms
15,704 KB
testcase_09 AC 406 ms
15,656 KB
testcase_10 AC 826 ms
15,836 KB
testcase_11 AC 289 ms
15,928 KB
testcase_12 AC 510 ms
15,972 KB
testcase_13 AC 869 ms
16,044 KB
testcase_14 AC 93 ms
15,604 KB
testcase_15 AC 1,183 ms
15,900 KB
testcase_16 AC 1,233 ms
15,964 KB
testcase_17 AC 73 ms
15,308 KB
testcase_18 AC 74 ms
15,148 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:1: warning: assigned but unused variable - n
Syntax OK

ソースコード

diff #

n, k = gets.chomp.split(" ").map(&:to_i)
s = gets.chomp

h = Hash.new

s.length.times do |i|
  if s[i] == '('
    h[i] = -1
  else
    h.reverse_each do |key, value|
      if value == -1
        h[key] = i
        break
      end
    end
  end
end

if h.keys.include?(k-1)
  puts h[k-1] + 1
else # 値からキーを取得
  puts h.key(k-1) + 1
end
0