結果
| 問題 |
No.1646 Avoid Palindrome
|
| コンテスト | |
| ユーザー |
sotanishy
|
| 提出日時 | 2021-08-14 23:06:22 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,929 ms / 3,000 ms |
| コード長 | 3,288 bytes |
| コンパイル時間 | 2,003 ms |
| コンパイル使用メモリ | 197,116 KB |
| 最終ジャッジ日時 | 2025-01-23 22:15:46 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 40 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template <int mod>
class Modint {
using mint = Modint;
static_assert(mod > 0, "Modulus must be positive");
public:
static constexpr int get_mod() noexcept { return mod; }
constexpr Modint(long long y = 0) noexcept : x(y >= 0 ? y % mod : (y % mod + mod) % mod) {}
constexpr int value() const noexcept { return x; }
constexpr mint& operator+=(const mint& r) noexcept { if ((x += r.x) >= mod) x -= mod; return *this; }
constexpr mint& operator-=(const mint& r) noexcept { if ((x += mod - r.x) >= mod) x -= mod; return *this; }
constexpr mint& operator*=(const mint& r) noexcept { x = static_cast<int>(1LL * x * r.x % mod); return *this; }
constexpr mint& operator/=(const mint& r) noexcept { *this *= r.inv(); return *this; }
constexpr mint operator-() const noexcept { return mint(-x); }
constexpr mint operator+(const mint& r) const noexcept { return mint(*this) += r; }
constexpr mint operator-(const mint& r) const noexcept { return mint(*this) -= r; }
constexpr mint operator*(const mint& r) const noexcept { return mint(*this) *= r; }
constexpr mint operator/(const mint& r) const noexcept { return mint(*this) /= r; }
constexpr bool operator==(const mint& r) const noexcept { return x == r.x; }
constexpr bool operator!=(const mint& r) const noexcept { return x != r.x; }
constexpr mint inv() const noexcept {
int a = x, b = mod, u = 1, v = 0;
while (b > 0) {
int t = a / b;
std::swap(a -= t * b, b);
std::swap(u -= t * v, v);
}
return mint(u);
}
constexpr mint pow(long long n) const noexcept {
mint ret(1), mul(x);
while (n > 0) {
if (n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend std::ostream& operator<<(std::ostream& os, const mint& r) {
return os << r.x;
}
friend std::istream& operator>>(std::istream& is, mint& r) {
long long t;
is >> t;
r = mint(t);
return is;
}
private:
int x;
};
using mint = Modint<998244353>;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
string S;
cin >> S;
if (N == 1) {
if (S[0] == '?') {
cout << 26 << endl;
} else {
cout << 1 << endl;
}
return 0;
}
vector<mint> dp(26*26);
for (int a = 0; a < 26; ++a) {
if (S[0] != '?' && S[0] != 'a' + a) continue;
for (int b = 0; b < 26; ++b) {
if (S[1] != '?' && S[1] != 'a' + b) continue;
if (a != b) dp[26*a + b] = 1;
}
}
for (int i = 2; i < N; ++i) {
vector<mint> ndp(26*26);
for (int c = 0; c < 26; ++c) {
if (S[i] != '?' && S[i] != 'a' + c) continue;
for (int a = 0; a < 26; ++a) {
if (a == c) continue;
for (int b = 0; b < 26; ++b) {
if (b == c) continue;
ndp[26*b + c] += dp[26*a + b];
}
}
}
dp.swap(ndp);
}
mint ans = 0;
for (int i = 0; i < 26*26; ++i) ans += dp[i];
cout << ans << endl;
}
sotanishy