結果
問題 |
No.345 最小チワワ問題
|
ユーザー |
![]() |
提出日時 | 2025-08-13 18:18:05 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,476 bytes |
コンパイル時間 | 509 ms |
コンパイル使用メモリ | 82,056 KB |
実行使用メモリ | 63,948 KB |
最終ジャッジ日時 | 2025-08-13 18:18:09 |
合計ジャッジ時間 | 3,765 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 WA * 2 |
other | AC * 14 WA * 15 |
ソースコード
from collections.abc import Iterable from enum import IntEnum, auto from functools import cache class E(IntEnum): S = 0 C = auto() W1 = auto() W2 = auto() ANY = auto() def nexts(self): match self: case E.S: return [E.C] case E.C: return [E.W1, E.ANY] case E.W1: return [E.W2, E.ANY] case E.W2: return [E.ANY] case E.ANY: return [E.C, E.W1, E.W2] assert False @staticmethod @cache def states(): res = [] for fm in E: for to in fm.nexts(): res.append((fm, to)) return res def state_dp(xs: Iterable, op, e, init: dict, *, is_reset=True): dp = [e] * len(E) for k, v in init.items(): dp[k] = v for x in xs: pp = [e] * len(E) if is_reset else dp.copy() dp, pp = pp, dp for fm, to in E.states(): if not is_valid(to, pp[fm], x): continue dp[to] = op(to, dp[to], fm, pp[fm], x) return dp def is_valid(to: E, fm_v, v) -> bool: if to == E.C: return v == 'c' if fm_v == INF: return False match to: case E.C: return v == 'c' case E.W1 | E.W2: return v == 'w' return True def op(to: E, to_v, fm: E, fm_v, v): if to == E.C: return 1 return min(to_v, fm_v + 1) INF = 1 << 60 S = input() dp = state_dp(S, op, INF, {E.S: 0}) ans = dp[E.W2] if ans == INF: print(-1) else: print(ans)