結果

問題 No.205 マージして辞書順最小
コンテスト
ユーザー くわい
提出日時 2015-11-20 21:14:47
言語 Scala(Beta)
(3.8.1)
コンパイル:
scalac _filename_
実行:
java -cp .:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala3-library_3/3.8.1/scala3-library_3-3.8.1.jar:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala-library/3.8.1/scala-library-3.8.1.jar _class_
結果
AC  
実行時間 510 ms / 5,000 ms
コード長 834 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 10,503 ms
コンパイル使用メモリ 265,440 KB
実行使用メモリ 59,560 KB
最終ジャッジ日時 2026-03-09 14:23:08
合計ジャッジ時間 20,131 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import scala.annotation.tailrec
import scala.io.StdIn

object Problem205 {

  def proc(N: Int, S: Seq[String]): String = {
    @tailrec
    def rec(result: Seq[Char], S: Seq[String]): String = {
      if (S.isEmpty) return result.reverse.mkString("")

      val sorted = S.sorted
      val (next, others) = (sorted.head, sorted.tail)
      val nextChar: Char = next.head

      val remains: Seq[String] = next.tail match {
        case "" => others
        case x if x.startsWith("{") => others
        case x => (x + "{") +: others
      }

      rec(nextChar +: result, remains)
    }

    val padS = S.map(s => s + ("{" * (N - s.length)))
    rec(Nil, padS)
  }

  def main(args: Array[String]) = {
    val N = StdIn.readInt()
    val S = Seq.fill(N)(StdIn.readLine())
    val result: String = proc(N, S)
    println(result)
  }
}
0