結果

問題 No.1646 Avoid Palindrome
ユーザー brthyyjpbrthyyjp
提出日時 2022-01-20 15:51:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,203 ms / 3,000 ms
コード長 1,981 bytes
コンパイル時間 312 ms
コンパイル使用メモリ 87,316 KB
実行使用メモリ 80,148 KB
最終ジャッジ日時 2023-08-15 13:32:07
合計ジャッジ時間 55,171 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
71,404 KB
testcase_01 AC 99 ms
77,192 KB
testcase_02 AC 91 ms
71,640 KB
testcase_03 AC 103 ms
77,512 KB
testcase_04 AC 1,173 ms
79,432 KB
testcase_05 AC 1,175 ms
79,540 KB
testcase_06 AC 1,253 ms
79,808 KB
testcase_07 AC 1,164 ms
79,892 KB
testcase_08 AC 1,359 ms
79,696 KB
testcase_09 AC 1,174 ms
79,504 KB
testcase_10 AC 1,235 ms
80,148 KB
testcase_11 AC 1,176 ms
79,852 KB
testcase_12 AC 1,218 ms
79,628 KB
testcase_13 AC 1,175 ms
79,480 KB
testcase_14 AC 1,999 ms
79,608 KB
testcase_15 AC 2,008 ms
79,952 KB
testcase_16 AC 1,942 ms
79,716 KB
testcase_17 AC 2,035 ms
79,960 KB
testcase_18 AC 1,972 ms
79,596 KB
testcase_19 AC 2,004 ms
79,588 KB
testcase_20 AC 1,981 ms
79,888 KB
testcase_21 AC 2,106 ms
79,692 KB
testcase_22 AC 2,063 ms
79,668 KB
testcase_23 AC 2,125 ms
79,740 KB
testcase_24 AC 2,089 ms
79,740 KB
testcase_25 AC 2,165 ms
79,764 KB
testcase_26 AC 2,193 ms
79,656 KB
testcase_27 AC 2,203 ms
79,820 KB
testcase_28 AC 2,173 ms
79,832 KB
testcase_29 AC 844 ms
79,532 KB
testcase_30 AC 853 ms
79,528 KB
testcase_31 AC 879 ms
79,892 KB
testcase_32 AC 860 ms
79,604 KB
testcase_33 AC 804 ms
79,600 KB
testcase_34 AC 2,111 ms
78,996 KB
testcase_35 AC 81 ms
71,704 KB
testcase_36 AC 80 ms
71,512 KB
testcase_37 AC 81 ms
71,480 KB
testcase_38 AC 81 ms
71,484 KB
testcase_39 AC 81 ms
71,688 KB
testcase_40 AC 81 ms
71,532 KB
testcase_41 AC 82 ms
71,596 KB
testcase_42 AC 82 ms
71,688 KB
testcase_43 AC 790 ms
79,640 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 = Counter()
            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