結果

問題 No.1646 Avoid Palindrome
ユーザー brthyyjp
提出日時 2022-01-20 15:51:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,354 ms / 3,000 ms
コード長 1,981 bytes
コンパイル時間 185 ms
コンパイル使用メモリ 82,132 KB
実行使用メモリ 78,872 KB
最終ジャッジ日時 2024-11-23 20:57:20
合計ジャッジ時間 55,786 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

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