結果

問題 No.1646 Avoid Palindrome
ユーザー rin204
提出日時 2022-04-15 16:49:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,138 ms / 3,000 ms
コード長 1,198 bytes
コンパイル時間 245 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 79,616 KB
最終ジャッジ日時 2024-12-24 21:13:28
合計ジャッジ時間 23,426 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

def f(S):
    if S == "?":
        return -1
    else:
        return ord(S) - 97

n = int(input())
S = input()

if n == 1:
    if S == "?":
        print(26)
    else:
        print(1)
    exit()

S = list(map(f, S))
dp = [0] * (26 * 26)

if S[0] == -1:
    l1 = list(range(26))
else:
    l1 = [S[0]]

if S[1] == -1:
    l2 = list(range(26))
else:
    l2 = [S[1]]

for i in l1:
    for j in l2:
        if i != j:
            dp[i * 26 + j] = 1

for s in S[2:]:
    ndp = [0] * (26 * 26)
    cum = [0] * 26
    for i in range(26):
        for j in range(26):
            cum[j] += dp[26 * i + j]
    if s != -1:
        for i in range(26):
            for j in range(26):
                if i == j:
                    continue
                if s != -1:
                    if s != i and s != j:
                        ndp[26 * j + s] += dp[26 * i + j]
                        ndp[26 * j + s] %= MOD
    else:
        for j in range(26):
            for k in range(26):
                if j != k:
                    ndp[26 * j + k] += cum[j] - dp[26 * k + j]
                    ndp[26 * j + k] %= MOD
                

                    

    dp = ndp

print(sum(dp) % MOD)
0