結果

問題 No.22 括弧の対応
ユーザー tshigtshig
提出日時 2016-08-02 17:35:22
言語 Ruby
(3.3.0)
結果
AC  
実行時間 87 ms / 5,000 ms
コード長 512 bytes
コンパイル時間 51 ms
コンパイル使用メモリ 11,516 KB
実行使用メモリ 15,952 KB
最終ジャッジ日時 2023-09-27 13:07:03
合計ジャッジ時間 2,630 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
15,144 KB
testcase_01 AC 81 ms
15,260 KB
testcase_02 AC 80 ms
15,312 KB
testcase_03 AC 85 ms
15,952 KB
testcase_04 AC 84 ms
15,216 KB
testcase_05 AC 83 ms
15,316 KB
testcase_06 AC 86 ms
15,272 KB
testcase_07 AC 87 ms
15,560 KB
testcase_08 AC 81 ms
15,368 KB
testcase_09 AC 83 ms
15,404 KB
testcase_10 AC 83 ms
15,616 KB
testcase_11 AC 82 ms
15,224 KB
testcase_12 AC 82 ms
15,272 KB
testcase_13 AC 83 ms
15,392 KB
testcase_14 AC 82 ms
15,112 KB
testcase_15 AC 87 ms
15,652 KB
testcase_16 AC 85 ms
15,784 KB
testcase_17 AC 80 ms
15,292 KB
testcase_18 AC 80 ms
15,296 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Calc0022
  def initialize(args)
    args = args.map { |l| l.chomp.split(/\s+/) }
    @n, @k = args.shift.map(&:to_i)
    @s = args.shift.first
  end

  def run
    cs = @s.split(//).map { |c| c == '(' ? 1 : -1 }
    if cs[@k - 1] == 1
      find(cs[(@k - 1)..-1]) + @k
    else
      @k - find(cs[0..(@k - 1)].reverse)
    end
  end

  def find(cs)
    s = 0
    cs.each_with_index do |c, i|
      s += c
      return i if s == 0
    end
  end
end

puts Calc0022.new(STDIN.readlines).run if __FILE__ == $0
0