結果

問題 No.22 括弧の対応
ユーザー yukiccoyukicco
提出日時 2018-11-20 08:56:14
言語 Elixir
(1.16.2)
結果
AC  
実行時間 655 ms / 5,000 ms
コード長 954 bytes
コンパイル時間 1,417 ms
コンパイル使用メモリ 54,560 KB
実行使用メモリ 50,552 KB
最終ジャッジ日時 2023-08-29 23:08:54
合計ジャッジ時間 14,967 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 615 ms
49,120 KB
testcase_01 AC 628 ms
49,404 KB
testcase_02 AC 636 ms
49,288 KB
testcase_03 AC 638 ms
49,844 KB
testcase_04 AC 655 ms
49,976 KB
testcase_05 AC 634 ms
49,372 KB
testcase_06 AC 639 ms
49,484 KB
testcase_07 AC 635 ms
49,548 KB
testcase_08 AC 626 ms
50,000 KB
testcase_09 AC 623 ms
49,676 KB
testcase_10 AC 631 ms
49,556 KB
testcase_11 AC 618 ms
50,412 KB
testcase_12 AC 623 ms
49,496 KB
testcase_13 AC 617 ms
50,528 KB
testcase_14 AC 621 ms
49,764 KB
testcase_15 AC 629 ms
50,552 KB
testcase_16 AC 623 ms
49,512 KB
testcase_17 AC 622 ms
50,028 KB
testcase_18 AC 626 ms
49,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

defmodule Main do
    def paren(str, n) do
        left_fn =
            fn ->
                s = str |> String.slice((n)..-1)
                r = ~r/^(\(*)\(\)(.*)$/
                n + _paren(s, r)
            end
        right_fn =
            fn ->
                s = str |> String.slice(0..(n - 2))
                r = ~r/^(.*)\(\)(\)*)$/
                n - _paren(s, r)
            end
        n_char = str |> String.graphemes |> Enum.at(n - 1)
        case n_char do
            "(" -> left_fn.()
            ")" -> right_fn.()
        end
    end
    defp _paren(str, re, acc \\ 0) do
        case Regex.run(re, str, capture: :all_but_first) do
            nil -> acc + 1
            [s1, s2] -> _paren(s1 <> s2, re, acc + 2)
        end
    end
    def main do

        inputs = IO.binread(:all) |> String.split

        k = inputs |> Enum.at(1) |> String.to_integer
        s = inputs |> Enum.at(2)

        IO.puts paren(s, k)

    end
end
0