結果

問題 No.1646 Avoid Palindrome
ユーザー sgswsgsw
提出日時 2021-08-13 22:16:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,550 bytes
コンパイル時間 467 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 49,572 KB
最終ジャッジ日時 2024-04-14 18:26:55
合計ジャッジ時間 5,316 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
17,692 KB
testcase_01 AC 32 ms
10,880 KB
testcase_02 AC 30 ms
10,880 KB
testcase_03 AC 353 ms
12,032 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

'''
    Python3(PyPy3) Template for Programming-Contest.

    author : sgsw

    generated : 2021/08/13   
    when : 22:01:19

'''

from collections import defaultdict
import sys


def input():
    return sys.stdin.readline().rstrip()


DXY = [(0, -1), (1, 0), (0, 1), (-1, 0)]  # LDRU
mod = 998244353
inf = 1 << 64
Alphabets = "abcdefghijklmnopqrstuvwxyz"


def is_Palindrome(s: str) -> bool:
    n = len(s)
    for i in range(n//2):
        if s[i] != s[n - 1 - i]:
            return False
    return True


def main():
    n = int(input())
    s = "$" + input()

    #2文字と3文字を含まなければおけ
    #したがって直前の二個を持てば良い

    dp = [defaultdict(int) for i in range(n + 1)]
    ans = 0

    if s[1] != "?":
        dp[1][s[0] + s[1]] = 1
    else:
        for char in Alphabets:
            dp[1][s[0] + char] = 1

    for i in range(2,n + 1):
        char = s[i]
        if char != "?":
            c = char
            for k,v in dp[i - 1].items():
                if is_Palindrome(k + c) == False and k[1] != c:
                    dp[i][k[1] + c] += v
                    dp[i][k[1] + c] %= mod 
        else:
            for c in Alphabets:
                for k,v in dp[i - 1].items():
                    if is_Palindrome(k + c) == False and k[1] != c:
                        dp[i][k[1] + c] += v
                        dp[i][k[1] + c] %= mod 
    #find answer
    for k, v in dp[n].items():
        ans += v
    ans %= mod
    print(ans)
    return 0


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