結果

問題 No.1646 Avoid Palindrome
ユーザー mkawa2
提出日時 2021-08-14 05:58:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 785 ms / 3,000 ms
コード長 1,891 bytes
コンパイル時間 458 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 76,544 KB
最終ジャッジ日時 2024-11-08 16:15:36
合計ジャッジ時間 22,201 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = 10**16
md = 998244353
# md = 10**9+7

n = II()
s = SI()

if n == 1:
    if s[0] == "?":
        print(26)
    else:
        print(1)
    exit()

for i in range(n-1):
    if s[i] != "?" and s[i] == s[i+1]:
        print(0)
        exit()

for i in range(n-2):
    if s[i] != "?" and s[i] == s[i+2]:
        print(0)
        exit()

dp = [0]*26*26
col = [0]*26

if s[0] == "?" and s[1] == "?":
    for i in range(26):
        for j in range(26):
            if i == j: continue
            dp[i*26+j] = 1
            col[j] += 1

elif s[0] == "?":
    j = ord(s[1])-97
    for i in range(26):
        if i == j: continue
        dp[i*26+j] = 1
        col[j] += 1

elif s[1] == "?":
    i = ord(s[0])-97
    for j in range(26):
        if i == j: continue
        dp[i*26+j] = 1
        col[j] += 1
else:
    i = ord(s[0])-97
    j = ord(s[1])-97
    dp[i*26+j] = 1
    col[j] += 1

for c in s[2:]:
    a = ord(c)-97
    ndp = [0]*26*26
    ncl = [0]*26
    for i in range(26):
        for j in range(26):
            if i == j: continue
            if c == "?" or j == a:
                val = (col[i]-dp[j*26+i])%md
                ndp[i*26+j] = val
                ncl[j] += val

    dp = ndp
    col = ncl

ans = 0
for i in range(26):
    for j in range(26):
        ans += dp[i*26+j]
    ans %= md

print(ans)
0