結果
問題 | No.1646 Avoid Palindrome |
ユーザー | nawawan |
提出日時 | 2021-08-13 21:58:22 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 498 ms / 3,000 ms |
コード長 | 4,046 bytes |
コンパイル時間 | 2,456 ms |
コンパイル使用メモリ | 209,036 KB |
実行使用メモリ | 320,256 KB |
最終ジャッジ日時 | 2024-11-08 15:19:06 |
合計ジャッジ時間 | 18,314 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 427 ms
301,824 KB |
testcase_05 | AC | 425 ms
301,824 KB |
testcase_06 | AC | 412 ms
291,584 KB |
testcase_07 | AC | 428 ms
301,824 KB |
testcase_08 | AC | 430 ms
304,640 KB |
testcase_09 | AC | 411 ms
289,664 KB |
testcase_10 | AC | 421 ms
296,960 KB |
testcase_11 | AC | 409 ms
288,640 KB |
testcase_12 | AC | 428 ms
301,312 KB |
testcase_13 | AC | 432 ms
304,640 KB |
testcase_14 | AC | 458 ms
295,936 KB |
testcase_15 | AC | 465 ms
301,824 KB |
testcase_16 | AC | 454 ms
294,144 KB |
testcase_17 | AC | 473 ms
307,200 KB |
testcase_18 | AC | 462 ms
299,008 KB |
testcase_19 | AC | 457 ms
297,344 KB |
testcase_20 | AC | 460 ms
299,904 KB |
testcase_21 | AC | 468 ms
306,816 KB |
testcase_22 | AC | 456 ms
296,320 KB |
testcase_23 | AC | 475 ms
308,480 KB |
testcase_24 | AC | 492 ms
320,128 KB |
testcase_25 | AC | 489 ms
320,128 KB |
testcase_26 | AC | 490 ms
320,128 KB |
testcase_27 | AC | 494 ms
320,128 KB |
testcase_28 | AC | 498 ms
320,128 KB |
testcase_29 | AC | 426 ms
320,128 KB |
testcase_30 | AC | 428 ms
320,128 KB |
testcase_31 | AC | 425 ms
320,256 KB |
testcase_32 | AC | 428 ms
320,128 KB |
testcase_33 | AC | 423 ms
320,128 KB |
testcase_34 | AC | 491 ms
320,128 KB |
testcase_35 | AC | 2 ms
5,248 KB |
testcase_36 | AC | 2 ms
5,248 KB |
testcase_37 | AC | 2 ms
5,248 KB |
testcase_38 | AC | 2 ms
5,248 KB |
testcase_39 | AC | 2 ms
5,248 KB |
testcase_40 | AC | 2 ms
5,248 KB |
testcase_41 | AC | 2 ms
5,248 KB |
testcase_42 | AC | 2 ms
5,248 KB |
testcase_43 | AC | 426 ms
320,128 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; template<const int MOD> struct modint{ long long val; modint(): val(0) {} modint(long long x){ if(x < 0) val = x % MOD + MOD; else val = x % MOD; } modint(const modint &t){ val = t.val; } modint& operator =(const modint m){ val = m.val; return *this; } modint operator -(){ return modint(-val); } modint& operator-=(const modint &m){ val -= m.val; if(val < 0) val += MOD; return *this; } modint& operator+=(const modint &m){ val += m.val; if(val >= MOD) val -= MOD; return *this; } modint& operator*=(const modint &m){ val *= m.val; val %= MOD; return *this; } modint& operator/=(modint m){ *this *= m.inv(); return *this; } modint inv(){ long long x = 1, y = 0; long long a = val, b = MOD; while(b != 0){ long long t = a / b; a -= t * b; x -= t * y; swap(a, b); swap(x, y); } x %= MOD; if(x < 0) x += MOD; return modint(x); } modint pow(long long k){ long long res = 1; long long v = val; while(k > 0){ if(k & 1) res = res * v % MOD; v = v * v % MOD; k >>= 1; } return modint(res); } bool operator==(const modint &m){ return val == m.val; } modint operator+(const modint &m){ return modint(*this) += m; } modint operator-(const modint &m){ return modint(*this) -= m; } modint operator*(const modint &m){ return modint(*this) *= m; } modint operator/(const modint &m){ return modint(*this) /= m; } bool operator!=(const modint &m){ return modint(*this).val != m.val; } bool operator!=(const int &m){ return modint(*this).val != m; } }; const int MOD = 998244353; using mint = modint<MOD>; int main(){ int N; cin >> N; string S; cin >> S; if(N == 1){ if(S[0] == '?') cout << 26 << endl; else cout << 1 << endl; return 0; } vector<vector<vector<mint>>> dp(N, vector<vector<mint>>(26, vector<mint>(26))); if(S[0] == '?'){ if(S[1] == '?'){ for(int i = 0; i < 26; i++){ for(int j = 0; j < 26; j++){ if(i == j) continue; dp[1][i][j] = 1; } } } else{ for(int i = 0; i < 26; i++){ if(i == S[1] - 'a') continue; dp[1][S[1] - 'a'][i] = 1; } } } else{ if(S[1] == '?'){ for(int i = 0; i < 26; i++){ if(i == S[0] - 'a') continue; dp[1][i][S[0] - 'a'] = 1; } } else{ if(S[1] != S[0]) dp[1][S[1] - 'a'][S[0] - 'a'] = 1; } } for(int i = 2; i < N; i++){ if(S[i] == '?'){ for(int j = 0; j < 26; j++){ mint temp = 0; for(int k = 0; k < 26; k++) { if(j == k) continue; temp += dp[i - 1][j][k]; } for(int k = 0; k < 26; k++){ if(j == k) continue; dp[i][k][j] += temp - dp[i - 1][j][k]; } } } else{ int id = S[i] - 'a'; for(int j = 0; j < 26; j++){ if(id == j) continue; mint temp = 0; for(int k = 0; k < 26; k++){ if(k == id) continue; temp += dp[i - 1][j][k]; } dp[i][id][j] += temp; } } } mint ans = 0; for(int i = 0; i < 26; i++){ for(int j = 0; j < 26; j++){ ans += dp[N - 1][i][j]; } } cout << ans.val << endl; }