結果

問題 No.1646 Avoid Palindrome
コンテスト
ユーザー LyricalMaestro
提出日時 2026-07-18 19:30:18
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
TLE  
実行時間 -
コード長 2,011 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 222 ms
コンパイル使用メモリ 95,844 KB
実行使用メモリ 89,104 KB
最終ジャッジ日時 2026-07-18 19:30:24
合計ジャッジ時間 5,626 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other TLE * 1 -- * 39
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

## https://yukicoder.me/problems/no/1646

MOD = 998244353

def is_palindrome(word):
    left = 0
    right = len(word) - 1
    while left < right:
        if word[left] != word[right]:
            return False
        left += 1
        right -= 1
    return True

def main():
    N = int(input())
    S = input()

    # ありえる状態を番号づけ
    states_list = [""]
    for a in range(26):
        states_list.append(chr(a + ord("a")))
    for a in range(26):
        for b in range(26):
            x = chr(a + ord("a")) + chr(b + ord("a"))
            states_list.append(x)
    state_map = {}
    for from_i, before_word in enumerate(states_list):
        state_map[before_word] = from_i

    # before + word -> afterの対応表を作る
    state_trans_map =[[-1] * 26 for _ in range(len(states_list))]

    for from_i, before_word in enumerate(states_list):
        for a in range(26):
            word = before_word + chr(a + ord("a"))
            if len(word) >= 2 and is_palindrome(word):
                continue

            if len(word) > 2:
                word = word[-2:]
                
            if len(word) >= 2 and is_palindrome(word):
                continue

            after_i = state_map[word]
            state_trans_map[from_i][a] = after_i
    
    dp = [0] * len(states_list)
    dp[0] = 1
    for s in S:
        new_dp = [0] * len(states_list)        
        if s == "?":
            target = -1
        else:
            target = ord(s) - ord("a")
        
        for from_i in range(len(states_list)):
            for x in range(26):
                if target == -1 or target == x:
                    after_i = state_trans_map[from_i][x]
                    if after_i != -1:
                        new_dp[after_i] += dp[from_i]
                        new_dp[after_i] %= MOD
        dp = new_dp
    
    answer = 0
    for i in range(len(dp)):
        answer += dp[i]
        answer %= MOD
    print(answer)



    






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