結果

問題 No.345 最小チワワ問題
コンテスト
ユーザー tanson
提出日時 2026-06-03 10:35:11
言語 Standard ML
(MLton 20241230)
コンパイル:
mlton_wrapper _filename_
実行:
./main
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,388 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,437 ms
コンパイル使用メモリ 705,000 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-06-03 10:35:17
合計ジャッジ時間 5,290 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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 intString n =
    if 0 <= n
    then Int.toString n
    else "-" ^ Int.toString (abs n)


fun findAnsAux _ [] _ = NONE
  | findAnsAux i (#"c"::tl) acc = if acc = [] then findAnsAux (i + 1) tl [i]
                               else findAnsAux (i + 1) tl acc
  | findAnsAux i (#"w"::tl) acc = if acc = [] then findAnsAux (i + 1) tl []
                               else if List.length acc = 1 then findAnsAux (i + 1) tl (i::acc)
                               else if List.length acc = 2 then SOME (i - List.last acc + 1)
                               else findAnsAux (i + 1) tl acc
  | findAnsAux i (_::tl) acc = findAnsAux (i + 1) tl acc 

fun findAns [] NONE = ~1
  | findAns [] (SOME x) = x
  | findAns (h::tl) acc = 
    let
        val chiwawa = findAnsAux 0 (h::tl) []
    in
        case acc of
            NONE => findAns tl chiwawa
          | SOME accV => 
            case chiwawa of
                NONE => findAns tl acc
              | SOME chiwawaV => findAns tl (SOME (Int.min (accV, chiwawaV)))
    end



val () =
    let
        val s = readStr ()

        val ans = findAns (String.explode s) NONE
    in
        print (intString ans ^ "\n")
    end
0