3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

paiza S ランク問題を Elixir で解いてみます

文字列収集

指定された文字列で始まる文字列を買い占めます

defmodule Paiza do
  def main do
    [n | _m] =
      :stdio
      |> IO.read(:line)
      |> String.trim()
      |> String.split(" ")
      |> Enum.map(&String.to_integer(&1))

    {sp, q} =
      :stdio
      |> IO.read(:all)
      |> String.trim()
      |> String.split("\n")
      |> Enum.split(n)

    sp =
      sp
      |> Enum.map(fn line ->
        line
        |> String.split(" ")
        |> then(fn [s, p] ->
          {s, String.to_integer(p)}
        end)
      end)

    q
    |> Enum.map(fn q_i ->
      sp
      |> Enum.filter(fn {s, _p} ->
        String.starts_with?(s, q_i)
      end)
      |> Enum.map(fn {_s, p} ->
        p
      end)
      |> Enum.sum()
      |> IO.puts()
    end)
  end
end

Paiza.main()

解説

特に解説するほどの内容はないです

もっと入力行数が大きければ工夫が必要になってくると思いますが

まとめ

S ランク相当とは思えない問題でした

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?