結果

問題 No.1646 Avoid Palindrome
ユーザー mkawa2mkawa2
提出日時 2021-08-14 05:58:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 776 ms / 3,000 ms
コード長 1,891 bytes
コンパイル時間 515 ms
コンパイル使用メモリ 82,328 KB
実行使用メモリ 76,752 KB
最終ジャッジ日時 2024-04-26 03:45:57
合計ジャッジ時間 21,997 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
60,792 KB
testcase_01 AC 42 ms
60,992 KB
testcase_02 AC 37 ms
53,704 KB
testcase_03 AC 43 ms
61,440 KB
testcase_04 AC 716 ms
76,320 KB
testcase_05 AC 710 ms
76,152 KB
testcase_06 AC 693 ms
76,284 KB
testcase_07 AC 713 ms
76,180 KB
testcase_08 AC 666 ms
76,232 KB
testcase_09 AC 685 ms
76,228 KB
testcase_10 AC 691 ms
76,160 KB
testcase_11 AC 699 ms
76,504 KB
testcase_12 AC 736 ms
76,124 KB
testcase_13 AC 719 ms
76,552 KB
testcase_14 AC 483 ms
76,752 KB
testcase_15 AC 506 ms
76,408 KB
testcase_16 AC 503 ms
76,332 KB
testcase_17 AC 507 ms
76,344 KB
testcase_18 AC 490 ms
76,216 KB
testcase_19 AC 490 ms
76,408 KB
testcase_20 AC 498 ms
76,568 KB
testcase_21 AC 512 ms
76,520 KB
testcase_22 AC 504 ms
76,324 KB
testcase_23 AC 508 ms
76,252 KB
testcase_24 AC 534 ms
76,296 KB
testcase_25 AC 538 ms
76,388 KB
testcase_26 AC 526 ms
76,744 KB
testcase_27 AC 536 ms
76,420 KB
testcase_28 AC 537 ms
76,368 KB
testcase_29 AC 681 ms
76,388 KB
testcase_30 AC 622 ms
76,100 KB
testcase_31 AC 617 ms
76,160 KB
testcase_32 AC 622 ms
76,476 KB
testcase_33 AC 675 ms
76,176 KB
testcase_34 AC 776 ms
76,048 KB
testcase_35 AC 36 ms
53,820 KB
testcase_36 AC 36 ms
52,892 KB
testcase_37 AC 38 ms
53,700 KB
testcase_38 AC 37 ms
53,408 KB
testcase_39 AC 36 ms
54,012 KB
testcase_40 AC 36 ms
52,808 KB
testcase_41 AC 36 ms
53,240 KB
testcase_42 AC 37 ms
53,500 KB
testcase_43 AC 677 ms
76,156 KB
権限があれば一括ダウンロードができます

ソースコード

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