結果
| 問題 |
No.1646 Avoid Palindrome
|
| コンテスト | |
| ユーザー |
mkawa2
|
| 提出日時 | 2021-08-13 22:45:08 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,487 ms / 3,000 ms |
| コード長 | 2,762 bytes |
| コンパイル時間 | 3,950 ms |
| コンパイル使用メモリ | 251,544 KB |
| 最終ジャッジ日時 | 2025-01-23 20:28:54 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 40 |
ソースコード
#include <atcoder/all>
using namespace atcoder;
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
const int inf = 1e9;
const ll lim = 1e18;
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mod = 998244353;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod - a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint& operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream& operator>>(istream& is, mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }
mint dp[50005][26][26];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
string s;
cin >> n >> s;
if (n == 1) {
if (s[0] == '?')
cout << 26 << endl;
else
cout << 1 << endl;
return 0;
}
rep(i, n - 1) {
if (s[i] != '?') {
if (s[i] == s[i + 1] || (i + 2 < n && s[i] == s[i + 2])) {
cout << 0 << endl;
return 0;
}
}
}
if (s[0] == '?' && s[1] == '?') {
rep(j, 26) rep(k, 26) if (j != k) dp[2][j][k] = 1;
} else if (s[0] == '?') {
int k = s[1] - 'a';
rep(j, 26) if (j != k) dp[2][j][k] = 1;
} else if (s[1] == '?') {
int j = s[0] - 'a';
rep(k, 26) if (j != k) dp[2][j][k] = 1;
} else {
int j = s[0] - 'a';
int k = s[1] - 'a';
dp[2][j][k] = 1;
}
for (int i = 2; i < n; i++) {
rep(j, 26) rep(k, 26) {
mint pre = dp[i][j][k];
if (pre.x == 0) continue;
if (s[i] == '?') {
rep(nk, 26) if (nk != j && nk != k) dp[i+1][k][nk] += pre;
} else {
int nk = s[i] - 'a';
if (nk != j && nk != k) dp[i+1][k][nk] += pre;
}
}
}
mint ans;
rep(j, 26) rep(k, 26) ans += dp[n][j][k];
cout << ans << endl;
return 0;
}
mkawa2