結果

問題 No.1646 Avoid Palindrome
コンテスト
ユーザー LyricalMaestro
提出日時 2026-07-18 19:22:33
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
TLE  
実行時間 -
コード長 1,310 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,026 ms
コンパイル使用メモリ 96,112 KB
実行使用メモリ 119,468 KB
最終ジャッジ日時 2026-07-18 19:22:41
合計ジャッジ時間 7,868 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()

    dp = {"": 1}
    for s in S:
        new_dp = {}
        if s == "?":
            target = -1
        else:
            target = ord(s) - ord("a")

        for key, value in dp.items():
            for x in range(26):
                if target == -1 or target == x:
                    new_key = key + chr(x + ord("a"))
                    if len(new_key) >= 2 and is_palindrome(new_key):
                        continue
                    
                    if len(new_key) > 2:
                        new_key = new_key[-2:]
                    if len(new_key) == 2 and is_palindrome(new_key):
                        continue

                    if new_key not in new_dp:
                        new_dp[new_key] = 0
                    new_dp[new_key] += value
                    new_dp[new_key] %= MOD
        dp = new_dp

    answer = 0
    for value in dp.values():
        answer += value
        answer %= MOD
    print(answer)


    






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