結果
| 問題 |
No.345 最小チワワ問題
|
| コンテスト | |
| ユーザー |
norioc
|
| 提出日時 | 2025-08-13 18:34:11 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 57 ms / 2,000 ms |
| コード長 | 1,696 bytes |
| コンパイル時間 | 389 ms |
| コンパイル使用メモリ | 82,312 KB |
| 実行使用メモリ | 64,156 KB |
| 最終ジャッジ日時 | 2025-08-13 18:34:15 |
| 合計ジャッジ時間 | 3,570 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
from collections.abc import Iterable
from enum import IntEnum, auto
from functools import cache
class E(IntEnum):
S = 0
S_X = auto()
C = auto()
C_X = auto()
W1 = auto()
W1_X = auto()
W2 = auto()
G = auto()
def nexts(self):
match self:
case E.S: return [E.C, E.S_X]
case E.S_X: return [E.C, E.S_X]
case E.C: return [E.W1, E.C_X]
case E.C_X: return [E.W1, E.C_X]
case E.W1: return [E.W2, E.W1_X]
case E.W1_X: return [E.W2, E.W1_X]
case E.W2: return [E.G]
case E.G: return [E.G]
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 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 and v == 'c':
return 1
if to == E.G:
return min(to_v, fm_v)
return min(to_v, fm_v + 1)
INF = 1 << 60
S = input()
dp = state_dp(S, op, INF, {E.S: 0})
ans = min(dp[E.W2], dp[E.G])
if ans == INF:
print(-1)
else:
print(ans)
norioc