結果

問題 No.1646 Avoid Palindrome
ユーザー brthyyjpbrthyyjp
提出日時 2022-01-20 15:53:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 817 ms / 3,000 ms
コード長 1,978 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 87,204 KB
実行使用メモリ 79,484 KB
最終ジャッジ日時 2023-08-15 13:33:22
合計ジャッジ時間 25,213 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
71,688 KB
testcase_01 AC 90 ms
77,356 KB
testcase_02 AC 83 ms
71,560 KB
testcase_03 AC 88 ms
77,228 KB
testcase_04 AC 569 ms
78,568 KB
testcase_05 AC 574 ms
78,572 KB
testcase_06 AC 668 ms
78,772 KB
testcase_07 AC 553 ms
78,772 KB
testcase_08 AC 705 ms
78,696 KB
testcase_09 AC 530 ms
78,772 KB
testcase_10 AC 682 ms
78,864 KB
testcase_11 AC 552 ms
79,048 KB
testcase_12 AC 564 ms
78,636 KB
testcase_13 AC 599 ms
78,936 KB
testcase_14 AC 747 ms
78,888 KB
testcase_15 AC 759 ms
78,604 KB
testcase_16 AC 746 ms
78,396 KB
testcase_17 AC 765 ms
78,856 KB
testcase_18 AC 771 ms
79,008 KB
testcase_19 AC 739 ms
78,752 KB
testcase_20 AC 746 ms
78,560 KB
testcase_21 AC 770 ms
78,716 KB
testcase_22 AC 758 ms
78,812 KB
testcase_23 AC 771 ms
78,664 KB
testcase_24 AC 813 ms
78,692 KB
testcase_25 AC 807 ms
78,704 KB
testcase_26 AC 817 ms
78,996 KB
testcase_27 AC 803 ms
78,536 KB
testcase_28 AC 790 ms
78,888 KB
testcase_29 AC 640 ms
78,900 KB
testcase_30 AC 647 ms
79,484 KB
testcase_31 AC 673 ms
79,324 KB
testcase_32 AC 652 ms
79,048 KB
testcase_33 AC 602 ms
78,988 KB
testcase_34 AC 773 ms
78,136 KB
testcase_35 AC 81 ms
71,580 KB
testcase_36 AC 80 ms
71,768 KB
testcase_37 AC 80 ms
71,328 KB
testcase_38 AC 80 ms
71,704 KB
testcase_39 AC 82 ms
71,612 KB
testcase_40 AC 80 ms
71,704 KB
testcase_41 AC 82 ms
71,336 KB
testcase_42 AC 82 ms
71,692 KB
testcase_43 AC 622 ms
78,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = sys.stdin.readline

from collections import Counter

mod = 998244353

def main():

    n = int(input())
    s = str(input().rstrip())

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

    dp = [0]*(26*26)
    if s[0] != '?' and s[1] != '?':
        c0 = ord(s[0])-ord('a')
        c1 = ord(s[1])-ord('a')
        if c0 == c1:
            print(0)
            exit()
        dp[c1*26+c0] = 1
    elif s[0] != '?':
        c0 = ord(s[0])-ord('a')
        for c1 in range(26):
            if c0 == c1:
                continue
            dp[c1*26+c0] = 1
    elif s[1] != '?':
        c1 = ord(s[1])-ord('a')
        for c0 in range(26):
            if c0 == c1:
                continue
            dp[c1*26+c0] = 1
    else:
        for c0 in range(26):
            for c1 in range(26):
                if c0 == c1:
                    continue
                dp[c1*26+c0] = 1

    for i in range(2, n):
        nx = [0]*(26*26)
        c = s[i]
        if c == '?':
            d1 = [0]*26
            for c0 in range(26):
                for c1 in range(26):
                    d1[c1] += dp[c1*26+c0]
                    d1[c1] %= mod
            for c in range(26):
                for c1 in range(26):
                    if c == c1:
                        continue
                    nx[c*26+c1] += d1[c1]-dp[c1*26+c]
                    nx[c*26+c1] %= mod
        else:
            c = ord(c)-ord('a')
            for c0 in range(26):
                if c0 == c:
                    continue
                for c1 in range(26):
                    if c1 == c:
                        continue
                    nx[c*26+c1] += dp[c1*26+c0]
                    nx[c*26+c1] %= mod
        dp = nx
    ans = 0
    for c0 in range(26):
        for c1 in range(26):
            ans += dp[c1*26+c0]
            ans %= mod
    print(ans)

if __name__ == '__main__':
    main()
0