結果
| 問題 |
No.1646 Avoid Palindrome
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-13 22:26:47 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 666 ms / 3,000 ms |
| コード長 | 1,504 bytes |
| コンパイル時間 | 324 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 76,672 KB |
| 最終ジャッジ日時 | 2024-11-08 15:38:33 |
| 合計ジャッジ時間 | 20,422 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 40 |
ソースコード
n = int(input())
S = input()
mod = 998244353
dp = [0]*(26**2)
if len(S) == 1:
if S == "?":
ans = 26
else:
ans = 1
print(ans)
exit()
if S[0] == "?":
if S[1] == "?":
for i in range(26):
for j in range(26):
if i == j:
continue
dp[i*26+j] += 1
else:
c = ord(S[1])-ord("a")
for i in range(26):
if i == c:
continue
dp[i*26+c] += 1
else:
c = ord(S[0])-ord("a")
if S[1] == "?":
for j in range(26):
if c == j:
continue
dp[c*26+j] += 1
else:
c2 = ord(S[1])-ord("a")
if c != c2:
dp[c*26+c2] += 1
base = [0]*26
for s in S[2:]:
nbase = [0]*26
ndp = [0]*(26**2)
if s == "?":
for i in range(26**2):
x,y = divmod(i,26)
dp[i] += base[x]
nbase[y] += dp[i]
nbase[y] %= mod
ndp[y*26+y] -= dp[i]
ndp[y*26+y] %= mod
ndp[y*26+x] -= dp[i]
ndp[y*26+x] %= mod
else:
c = ord(s)-ord("a")
for i in range(26**2):
x,y = divmod(i,26)
dp[i] += base[x]
if x == c or y == c:
continue
ndp[y*26+c] += dp[i]
ndp[y*26+c] %= mod
dp = ndp
base = nbase
ans = 0
for i in range(26):
for j in range(26):
ans += dp[i*26+j]+base[i]
ans %= mod
print(ans)