結果

問題 No.1740 Alone 'a'
ユーザー norioc
提出日時 2025-05-20 21:19:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 699 ms / 2,000 ms
コード長 1,101 bytes
コンパイル時間 410 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 127,440 KB
最終ジャッジ日時 2025-05-20 21:19:42
合計ジャッジ時間 13,690 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

def ndlist(shape: list[int], *, val=0) -> list:
    assert len(shape) > 0 and all(s > 0 for s in shape)

    def rec(p):
        if p == len(shape)-1:
            return [val] * shape[p]

        return [rec(p+1) for _ in range(shape[p])]

    return rec(0)


def digit_dp(s: str) -> int:
    ds = [ord(c)-ord('a') for c in s]
    nd = len(ds)

    dp = ndlist([nd+1, 2, 2])
    # dp[i][j][k]
    # i : i 桁目までみた
    # j : n 未満か(j=0 完全一致 j=1 より小さい)
    # k : a をちょうど 1 つ使ったか
    dp[0][0][0] = 1

    for i in range(nd):
        for j in range(2):
            to = ds[i] if j == 0 else 25
            for k in range(2):  # a をちょうどひとつ使ったか
                for x in range(to+1):
                    if k > 0 and x == 0: continue  # a はひとつのみ
                    nj = j | (x < to)
                    nk = k | (x == 0)

                    dp[i+1][nj][nk] += dp[i][j][k]
                    dp[i+1][nj][nk] %= MOD

    return dp[nd][1][1]


MOD = 998244353
N = int(input())
S = input()

ans = digit_dp(S)
print(ans)
0