結果

問題 No.1646 Avoid Palindrome
ユーザー ygd.ygd.
提出日時 2021-08-13 22:50:39
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,852 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 82,436 KB
実行使用メモリ 89,296 KB
最終ジャッジ日時 2024-04-14 19:33:37
合計ジャッジ時間 4,927 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
69,008 KB
testcase_01 AC 44 ms
60,664 KB
testcase_02 AC 44 ms
59,852 KB
testcase_03 AC 64 ms
75,648 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 #

def main():
    N = int(input()); MOD = 998244353
    S = str(input())
    if N == 1:
        if S == "?":
            print(26)
        else:
            print(1)
        exit()

    MAX = 26*26
    #dp[i]: 一つ前がi//26,二つ前がi%26の場合    
    dp = [0]*MAX
    dp_pre = [0]*26
    dp_prepre = [0]*26
    for i in range(26): #二つ前
        s0 = chr(i+97)
        if S[0] == s0 or S[0] == "?":
            for j in range(26): #一つ前
                s1 = chr(j+97)
                if S[1] == s1 or S[1] == "?":
                    idx = j*26 + i
                    dp[idx] += 1
    #for i in range(26):
    #    for j in range(26):
    #        dp_pre[j] += dp[i][j]
    #        dp_prepre[i] += dp[i][j]
    #print(dp)
    for i in range(2,N):
        p = [0]*MAX
        p,dp = dp,p
        if S[i] != "?":
            si = ord(S[i]) - 97
            for pre in range(26):
                if si == pre: continue #一つ前と同じではだめ
                nidx = si*26 + pre
                for prepre in range(26):
                    if si == prepre: continue #二つ前と同じでもだめ
                    if pre == prepre: continue
                    preidx = pre*26 + prepre
                    dp[nidx] += p[preidx]
                    dp[nidx] %= MOD
        else: #?
            for now in range(26):
                for pre in range(26):
                    if now == pre: continue
                    for prepre in range(26):
                        if now == prepre: continue
                        if pre == prepre: continue
                        nidx = now*26 + pre
                        preidx = pre*26 + prepre
                        dp[nidx] += p[preidx]
                        dp[nidx] %= MOD

        #print(dp)
    
    ans = sum(dp)%MOD
    print(ans)


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