結果

問題 No.1646 Avoid Palindrome
ユーザー mkawa2mkawa2
提出日時 2021-08-13 22:45:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,276 ms / 3,000 ms
コード長 2,762 bytes
コンパイル時間 4,663 ms
コンパイル使用メモリ 254,768 KB
実行使用メモリ 267,948 KB
最終ジャッジ日時 2024-04-26 03:21:13
合計ジャッジ時間 49,634 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 174 ms
267,656 KB
testcase_01 AC 174 ms
267,720 KB
testcase_02 AC 173 ms
267,700 KB
testcase_03 AC 173 ms
267,600 KB
testcase_04 AC 450 ms
267,752 KB
testcase_05 AC 450 ms
267,656 KB
testcase_06 AC 438 ms
267,864 KB
testcase_07 AC 444 ms
267,904 KB
testcase_08 AC 445 ms
267,520 KB
testcase_09 AC 431 ms
267,644 KB
testcase_10 AC 437 ms
267,748 KB
testcase_11 AC 428 ms
267,640 KB
testcase_12 AC 449 ms
267,948 KB
testcase_13 AC 447 ms
267,672 KB
testcase_14 AC 2,086 ms
267,716 KB
testcase_15 AC 2,134 ms
267,736 KB
testcase_16 AC 2,087 ms
267,820 KB
testcase_17 AC 2,166 ms
267,760 KB
testcase_18 AC 2,110 ms
267,628 KB
testcase_19 AC 2,099 ms
267,808 KB
testcase_20 AC 2,119 ms
267,676 KB
testcase_21 AC 2,161 ms
267,772 KB
testcase_22 AC 2,112 ms
267,784 KB
testcase_23 AC 2,174 ms
267,752 KB
testcase_24 AC 2,242 ms
267,680 KB
testcase_25 AC 2,249 ms
267,664 KB
testcase_26 AC 2,247 ms
267,624 KB
testcase_27 AC 2,269 ms
267,908 KB
testcase_28 AC 2,263 ms
267,656 KB
testcase_29 AC 233 ms
267,752 KB
testcase_30 AC 235 ms
267,672 KB
testcase_31 AC 230 ms
267,772 KB
testcase_32 AC 238 ms
267,704 KB
testcase_33 AC 241 ms
267,684 KB
testcase_34 AC 2,276 ms
267,700 KB
testcase_35 AC 182 ms
267,672 KB
testcase_36 AC 182 ms
267,556 KB
testcase_37 AC 177 ms
267,560 KB
testcase_38 AC 176 ms
267,728 KB
testcase_39 AC 174 ms
267,640 KB
testcase_40 AC 173 ms
267,640 KB
testcase_41 AC 193 ms
267,752 KB
testcase_42 AC 178 ms
267,608 KB
testcase_43 AC 234 ms
267,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0