結果

問題 No.1646 Avoid Palindrome
コンテスト
ユーザー convexineq
提出日時 2021-08-13 23:40:22
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 691 ms / 3,000 ms
コード長 977 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 398 ms
コンパイル使用メモリ 84,992 KB
実行使用メモリ 82,176 KB
最終ジャッジ日時 2026-05-08 18:43:43
合計ジャッジ時間 17,191 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

MOD = 998244353
n = int(input())
s = [ord(i)-97 for i in input()]
if len(s)==1:
    print(26 if s[0] < 0 else 1)
    exit()
M = 26
dp = [[0]*M for _ in range(M)]
for i in range(M):
    for j in range(M):
        if i != j and (s[0] == i or s[0] < 0) and (s[1] == j or s[1] < 0):
            dp[i][j] += 1

for v in s[2:]:
    ndp = [[0]*M for _ in range(M)]
    if v < 0:
        c = [0]*M
        for i in range(M):
            for j in range(M):
                if i==j: continue
                c[j] += dp[i][j]
                ndp[j][i] -= dp[i][j]
        for i in range(M):
            for j in range(M):
                if i==j: continue
                ndp[i][j] += c[i]
                ndp[i][j] %= MOD
    else:
        for i in range(M):
            if i == v: continue
            for j in range(M):
                if j == v: continue
                ndp[j][v] += dp[i][j]
                ndp[j][v] %= MOD
    dp = ndp        

print(sum(sum(di) for di in dp)%MOD)
0