結果

問題 No.1646 Avoid Palindrome
ユーザー ygd.
提出日時 2021-08-13 22:50:39
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,852 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 82,732 KB
実行使用メモリ 89,256 KB
最終ジャッジ日時 2024-10-03 21:26:34
合計ジャッジ時間 4,890 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other TLE * 1 -- * 39
権限があれば一括ダウンロードができます

ソースコード

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