結果

問題 No.1646 Avoid Palindrome
ユーザー ST_BlackericST_Blackeric
提出日時 2022-12-26 01:05:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,363 bytes
コンパイル時間 1,893 ms
コンパイル使用メモリ 176,700 KB
実行使用メモリ 9,764 KB
最終ジャッジ日時 2023-08-12 16:56:43
合計ジャッジ時間 42,764 ms
ジャッジサーバーID
(参考情報)
judge9 / judge8
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 827 ms
9,420 KB
testcase_05 AC 823 ms
9,396 KB
testcase_06 AC 796 ms
9,112 KB
testcase_07 AC 824 ms
9,428 KB
testcase_08 AC 830 ms
9,492 KB
testcase_09 AC 787 ms
9,108 KB
testcase_10 AC 805 ms
9,504 KB
testcase_11 AC 786 ms
9,172 KB
testcase_12 AC 829 ms
9,440 KB
testcase_13 AC 834 ms
9,444 KB
testcase_14 AC 1,599 ms
9,116 KB
testcase_15 AC 1,626 ms
9,380 KB
testcase_16 AC 1,591 ms
9,136 KB
testcase_17 AC 1,664 ms
9,428 KB
testcase_18 AC 1,619 ms
9,484 KB
testcase_19 AC 1,606 ms
9,396 KB
testcase_20 AC 1,623 ms
9,424 KB
testcase_21 AC 1,659 ms
9,436 KB
testcase_22 AC 1,601 ms
9,108 KB
testcase_23 AC 1,668 ms
9,648 KB
testcase_24 AC 1,727 ms
9,704 KB
testcase_25 AC 1,739 ms
9,652 KB
testcase_26 AC 1,733 ms
9,696 KB
testcase_27 AC 1,732 ms
9,656 KB
testcase_28 AC 1,732 ms
9,744 KB
testcase_29 AC 365 ms
9,652 KB
testcase_30 AC 366 ms
9,692 KB
testcase_31 AC 363 ms
9,764 KB
testcase_32 AC 366 ms
9,656 KB
testcase_33 AC 366 ms
9,684 KB
testcase_34 AC 1,730 ms
9,672 KB
testcase_35 RE -
testcase_36 RE -
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 1 ms
4,384 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 1 ms
4,388 KB
testcase_42 AC 2 ms
4,384 KB
testcase_43 AC 373 ms
9,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>; using Pll = pair<ll, ll>;
using vi = vector<int>; using vvi = vector<vector<int>>; using vvvi = vector<vvi>;
using vl = vector<ll>; using vvl = vector<vector<ll>>; using vvvl = vector<vvl>;
using vb = vector<bool>; using vvb = vector<vector<bool>>;
using vp = vector<P>; using vpll = vector<Pll>; using vvp = vector<vector<P>>;
using vs = vector<string>;
#define rep(i, s, n) for (long long i = (s); i < (long long)(n); i++)
#define drep(i, s, n) for (long long i = (s); i >= (long long)(n); i--)
template <typename T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }
template <typename T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
const int INF = 1001001001;
const ll LLINF = 1e18;

// Mod int
using ull = unsigned long long; using uint = unsigned;
#define pb push_back
const uint mod = 998244353;
//const uint mod = 1000000007;
struct mint {
    uint x;
    mint() : x(0) {}
    mint(ll x) :x((x% mod + mod) % mod) {}
    mint operator-() const { return mint(0) - *this; }
    mint operator~() const { return mint(1) / *this; }
    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 = (ull)x * a.x % mod; return *this; }
    mint& operator/=(const mint& a) { x = (ull)x * a.pow(mod - 2).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 operator/(const mint& a) const { return mint(*this) /= a; }
    mint pow(ll t) const {
        mint res = 1; for (mint p = x; t; p *= p, t >>= 1) if (t & 1) res *= p; return res;
    }
    mint ppow(ll t) const { int p = mod - 1; return pow((t % p + p) % p); }
    bool operator<(const mint& a) const { return x < a.x; }
    bool operator==(const mint& a) const { return x == a.x; }
    bool operator!=(const mint& a) const { return x != a.x; }
};
mint ex(mint x, ll t) { return x.pow(t); }
istream& operator>>(istream& i, mint& a) { i >> a.x; return i; }
//*
ostream& operator<<(ostream& o, const mint& a) { o << a.x; return o; }
using vm = vector<mint>;
using vvm = vector<vm>;
using vvvm = vector<vvm>;
struct modinv {
    int n; vm d;
    modinv() : n(2), d({ 0,1 }) {}
    mint operator()(int i) { while (n <= i) d.pb(-d[mod % n] * (mod / n)), ++n; return d[i]; }
    mint operator[](int i) const { return d[i]; }
} invs;
struct modfact {
    int n; vm d;
    modfact() : n(2), d({ 1,1 }) {}
    mint operator()(int i) { while (n <= i) d.pb(d.back() * n), ++n; return d[i]; }
    mint operator[](int i) const { return d[i]; }
} facs;
struct modfactinv {
    int n; vm d;
    modfactinv() : n(2), d({ 1,1 }) {}
    mint operator()(int i) { while (n <= i) d.pb(d.back() * invs(n)), ++n; return d[i]; }
    mint operator[](int i) const { return d[i]; }
} ifacs;
mint comb(int a, int b) {
    if (a < b || b < 0) return 0;
    return facs(a) * ifacs(b) * ifacs(a - b);
}
mint perm(int a, int b) {
    if (a < b || b < 0) return 0;
    return  facs(a) * ifacs(a - b);
}
//

int main()
{
    int n; string s;
    cin >> n >> s;

    vvi ch(n, vi(26, 1));
    
    rep(i, 0, n) if(s[i] != '?')
    {
        if (i - 1 >= 0) ch[i - 1][s[i] - 'a'] &= 0;
        if (i - 2 >= 0) ch[i - 2][s[i] - 'a'] &= 0;
        if (i + 1 < n) ch[i + 1][s[i] - 'a'] &= 0;
        if (i + 2 < n) ch[i + 2][s[i] - 'a'] &= 0;
        rep(j, 0, 26)
        {
            if (j != s[i] - 'a') ch[i][j] &= 0;
        }
    }

    vvm dp(26, vm(26, 0));
   
    rep(i, 0, 26) rep(j, 0, 26)
    {
        if (i == j) continue;
        if (!ch[0][i] || !ch[1][j]) continue;
        dp[i][j] = 1;
    }

    rep(i, 2, n)
    {
        vvm dq(26, vm(26, 0));
        rep(j, 0, 26) rep(k, 0, 26)
        {
            if (j == k) continue;
            if (!ch[i][k]) continue;

            rep(x, 0, 26)
            {
                if (x == k) continue;
                dq[j][k] += dp[x][j];
            }
        }
        swap(dp, dq);
    }

    mint ans = 0;
    rep(i, 0, 26) rep(j, 0, 26) ans += dp[i][j];
    cout << ans;
}
0