結果

問題 No.3210 Fixed Sign Sequense
ユーザー norioc
提出日時 2025-07-26 18:52:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 1,452 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 82,780 KB
実行使用メモリ 78,260 KB
最終ジャッジ日時 2025-07-26 18:52:22
合計ジャッジ時間 8,444 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections.abc import Iterable
from enum import IntEnum, auto
from functools import cache


class E(IntEnum):
    S = 0
    NEG = auto()
    ZERO = auto()
    POS = auto()

    def nexts(self):
        match self:
            case E.S: return [E.NEG, E.ZERO, E.POS]
            case E.NEG: return [E.NEG, E.ZERO, E.POS]
            case E.ZERO: return [E.POS]
            case E.POS: return [E.POS]

        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:
    return True


def is_match(to: E, v):
    match to:
        case E.NEG: return v == '-'
        case E.ZERO: return v == '0'
        case E.POS: return v == '+'

    return False


def op(to: E, to_v, fm: E, fm_v, v):
    score = 1 if is_match(to, v) else 0
    return max(to_v, fm_v + score)


N = int(input())
S = input()

dp = state_dp(S, op, 0, {E.S: 0})
ans = max(dp[x] for x in [E.NEG, E.ZERO, E.POS])
print(ans)
0