結果
問題 |
No.1646 Avoid Palindrome
|
ユーザー |
![]() |
提出日時 | 2021-08-13 23:14:25 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,323 ms / 3,000 ms |
コード長 | 2,009 bytes |
コンパイル時間 | 329 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 77,312 KB |
最終ジャッジ日時 | 2024-11-08 16:03:47 |
合計ジャッジ時間 | 36,277 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
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): #二つ前 for j in range(26): if i == j: continue if S[0] == chr(i+97) or S[0] == "?": if S[1] == chr(j+97) or S[1] =="?": idx = j*26 + i dp[idx] = 1 for i in range(26): for j in range(26): idx = j*26 + i dp_pre[j] += dp[idx] #dp_prepre[i] += dp[idx] #print(dp) #print(dp_pre) #print(dp_prepre) for i in range(2,N): p = [0]*MAX p_pre = [0]*26 #p_prepre = [0]*26 p,dp = dp,p dp_pre,p_pre = p_pre,dp_pre #dp_prepre,p_prepre = p_prepre,dp_prepre #SUM = sum(p) if S[i] != "?": si = ord(S[i]) - 97 for pre in range(26): if si == pre: continue #一つ前と同じではだめ nidx = si*26 + pre dp[nidx] += p_pre[pre] - p[pre*26 + si] #print(dp[nidx]) dp[nidx] %= MOD else: #? for si in range(26): #今 for pre in range(26): #今の前 if si == pre: continue nidx = si*26 + pre dp[nidx] += p_pre[pre] - p[pre*26 + si] #print(dp[nidx]) dp[nidx] %= MOD for j in range(26): #二つ前 for k in range(26): #一つ前 if j == k: continue jkidx = k*26 + j dp_pre[k] += dp[jkidx] #dp_prepre[j] += dp[jkidx] #print(dp) #print(dp_pre) ans = sum(dp)%MOD print(ans) if __name__ == '__main__': main()