結果

問題 No.345 最小チワワ問題
コンテスト
ユーザー norioc
提出日時 2026-07-16 02:53:45
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 1,060 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 259 ms
コンパイル使用メモリ 95,976 KB
実行使用メモリ 85,120 KB
最終ジャッジ日時 2026-07-16 02:54:06
合計ジャッジ時間 5,684 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15 WA * 14
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from enum import Enum, auto


def accum_dp(xs: list, f, op, e, init: dict):
    dp = init.copy()
    for x in xs:
        pp = {}
        dp, pp = pp, dp
        for fm_key, fm_val in pp.items():
            for to_key, to_val in f(fm_key, fm_val, x):
                dp[to_key] = op(dp.get(to_key, e), to_val)

    return dp


class E(Enum):
    S = auto()
    C = auto()
    W1 = auto()
    W2 = auto()


def f(k, v, x):
    # k : (最後に選択した E, E.C からの長さ)
    state, w = k

    match (state, x):
        case (E.S, 'c'):
            yield (E.C, 1), v

        case (E.C, 'w'):
            yield (E.W1, w+1), v

        case (E.W1, 'w'):
            yield (E.W2, 0), w+1

        case (E.W2, _):  # goal
            yield (E.W2, 0), v

        case _:  # 現在の状態を維持
            yield (state, w+1), v


INF = 1 << 62
S = input()

init = {(E.S, 0): 0}
dp = accum_dp(S, f, min, INF, init)

ans = INF
for (st, _), v in dp.items():
    if st == E.W2:
        ans = min(ans, v)

if ans == INF:
    print(-1)
else:
    print(ans)
0