結果
問題 |
No.1646 Avoid Palindrome
|
ユーザー |
![]() |
提出日時 | 2021-08-13 22:12:39 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,568 bytes |
コンパイル時間 | 6,545 ms |
コンパイル使用メモリ | 76,724 KB |
実行使用メモリ | 368,400 KB |
最終ジャッジ日時 | 2024-10-03 19:28:29 |
合計ジャッジ時間 | 25,734 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 10 TLE * 1 -- * 29 |
ソースコード
import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); char[] s = sc.next().toCharArray(); sc.close(); if (n == 1) { if (s[0] == '?') { System.out.println(26); } else { System.out.println(1); } return; } int mod = 998244353; long[][][] dp = new long[n][26][26]; for (int j1 = 0; j1 < 26; j1++) { if (s[0] != '?' && s[0] - 'a' != j1) { continue; } for (int j2 = 0; j2 < 26; j2++) { if (s[1] != '?' && s[1] - 'a' != j2) { continue; } if (j1 == j2) { continue; } dp[1][j1][j2] = 1; } } for (int i = 2; i < n; i++) { for (int c = 0; c < 26; c++) { if (s[i] != '?' && s[i] - 'a' != c) { continue; } for (int j1 = 0; j1 < 26; j1++) { if (j1 == c) { continue; } if (s[i - 1] != '?' && s[i - 1] - 'a' != j1) { continue; } for (int j2 = 0; j2 < 26; j2++) { if (j2 == c) { continue; } if (s[i - 2] != '?' && s[i - 2] - 'a' != j2) { continue; } dp[i][j1][c] += dp[i - 1][j2][j1]; } } } for (int j1 = 0; j1 < 26; j1++) { for (int j2 = 0; j2 < 26; j2++) { dp[i][j1][j2] %= mod; } } } long ans = 0; for (int j1 = 0; j1 < 26; j1++) { if (s[n - 1] == '?') { for (int j2 = 0; j2 < 26; j2++) { ans += dp[n - 1][j1][j2]; } } else { ans += dp[n - 1][j1][s[n - 1] - 'a']; } } ans %= mod; System.out.println(ans); } }