結果
| 問題 | 
                            No.1646 Avoid Palindrome
                             | 
                    
| コンテスト | |
| ユーザー | 
                             brthyyjp
                         | 
                    
| 提出日時 | 2022-01-20 15:53:13 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 856 ms / 3,000 ms | 
| コード長 | 1,978 bytes | 
| コンパイル時間 | 218 ms | 
| コンパイル使用メモリ | 82,304 KB | 
| 実行使用メモリ | 77,824 KB | 
| 最終ジャッジ日時 | 2024-11-23 20:58:59 | 
| 合計ジャッジ時間 | 25,523 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 40 | 
ソースコード
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()
            
            
            
        
            
brthyyjp