結果

問題 No.39 桁の数字を入れ替え
コンテスト
ユーザー tanson
提出日時 2026-04-11 01:18:39
言語 Standard ML
(MLton 20241230)
コンパイル:
mlton_wrapper _filename_
実行:
./main
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,366 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,594 ms
コンパイル使用メモリ 704,744 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-11 01:18:44
合計ジャッジ時間 3,669 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

fun quicksort [] = []
  | quicksort (h::tl) =
    let 
        val (s, b) =
            List.foldl
                (fn (x, (small, big)) =>
                    if x <= h then (x::small, big)
                    else (small, x::big))
                ([], [])
                tl
    in
        (quicksort s) @ [h] @ (quicksort b)
    end


fun readStr () =
    let
        fun scan reader stream = SOME (StringCvt.splitl (not o Char.isSpace) reader (StringCvt.skipWS reader stream))
    in
        valOf (TextIO.scanStream scan TextIO.stdIn)
    end


fun replaceFromLast toRemove toAdd l =
    let
        fun replace [] = []
          | replace (h::tl) = 
            if h = toRemove then toAdd :: tl
            else h :: replace tl
    in
        List.rev (replace (List.rev l))
    end


fun findAns s =
    let
        val asInt = List.map (fn ch => Char.ord ch) (String.explode s)
        val sorted = List.rev (quicksort asInt)
       
        fun findAnsAux [] _ = []
          | findAnsAux _ [] = []
          | findAnsAux (oh::ot) (dh::dt) = 
            if oh <> dh then dh :: replaceFromLast dh oh ot
            else oh :: findAnsAux ot dt
    in
        String.implode (List.map (fn i => Char.chr i) (findAnsAux asInt sorted))
    end


val () =
    let
        val n = readStr ()

        val ans = findAns n
    in
        print (ans ^ "\n")
    end

0