結果

問題 No.1646 Avoid Palindrome
ユーザー snow39snow39
提出日時 2021-08-13 22:44:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,607 ms / 3,000 ms
コード長 3,668 bytes
コンパイル時間 2,223 ms
コンパイル使用メモリ 110,360 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-08 10:06:49
合計ジャッジ時間 30,777 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 300 ms
4,380 KB
testcase_05 AC 299 ms
4,376 KB
testcase_06 AC 288 ms
4,380 KB
testcase_07 AC 295 ms
4,380 KB
testcase_08 AC 298 ms
4,376 KB
testcase_09 AC 282 ms
4,380 KB
testcase_10 AC 287 ms
4,380 KB
testcase_11 AC 284 ms
4,380 KB
testcase_12 AC 301 ms
4,376 KB
testcase_13 AC 297 ms
4,376 KB
testcase_14 AC 1,452 ms
4,380 KB
testcase_15 AC 1,480 ms
4,376 KB
testcase_16 AC 1,445 ms
4,376 KB
testcase_17 AC 1,508 ms
4,376 KB
testcase_18 AC 1,462 ms
4,380 KB
testcase_19 AC 1,455 ms
4,380 KB
testcase_20 AC 1,468 ms
4,376 KB
testcase_21 AC 1,500 ms
4,376 KB
testcase_22 AC 1,455 ms
4,380 KB
testcase_23 AC 1,512 ms
4,380 KB
testcase_24 AC 1,568 ms
4,380 KB
testcase_25 AC 1,570 ms
4,376 KB
testcase_26 AC 1,565 ms
4,376 KB
testcase_27 AC 1,570 ms
4,380 KB
testcase_28 AC 1,568 ms
4,376 KB
testcase_29 AC 133 ms
4,380 KB
testcase_30 AC 133 ms
4,380 KB
testcase_31 AC 132 ms
4,380 KB
testcase_32 AC 132 ms
4,380 KB
testcase_33 AC 133 ms
4,380 KB
testcase_34 AC 1,607 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 133 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
template <class T>
void chmin(T& a, const T& b) {
    if (a > b) a = b;
}
template <class T>
void chmax(T& a, const T& b) {
    if (a < b) a = b;
}

template <long long mod>
class modint {
private:
    using T = long long;
    T a;

public:
    constexpr modint(const long long x = 0) noexcept : a((x % mod + mod) % mod) {}
    constexpr T& value() noexcept {
        return a;
    }
    constexpr const T& value() const noexcept {
        return a;
    }
    constexpr modint operator-() const noexcept {
        return modint(0) -= *this;
    }
    constexpr modint operator+(const modint& rhs) const noexcept {
        return modint(*this) += rhs;
    }
    constexpr modint operator-(const modint& rhs) const noexcept {
        return modint(*this) -= rhs;
    }
    constexpr modint operator*(const modint& rhs) const noexcept {
        return modint(*this) *= rhs;
    }
    constexpr modint operator/(const modint& rhs) const noexcept {
        return modint(*this) /= rhs;
    }
    constexpr modint& operator+=(const modint& rhs) noexcept {
        a += rhs.a;
        if (a >= mod) a -= mod;
        return *this;
    }
    constexpr modint& operator-=(const modint& rhs) noexcept {
        if (a < rhs.a) a += mod;
        a -= rhs.a;
        return *this;
    }
    constexpr modint& operator*=(const modint& rhs) noexcept {
        a = a * rhs.a % mod;
        return *this;
    }
    constexpr modint& operator/=(const modint& rhs) noexcept {
        return *this *= rhs.inv();
    }
    constexpr bool operator==(const modint& rhs) const noexcept {
        return a == rhs.a;
    }
    constexpr bool operator!=(const modint& rhs) const noexcept {
        return not(*this == rhs);
    }
    constexpr modint pow(long long k) const noexcept {
        modint ret(1);
        modint x = k > 0 ? *this : this->inv();
        k = abs(k);
        while (k > 0) {
            if (k & 1) ret *= x;
            x *= x;
            k >>= 1;
        }
        return ret;
    }
    constexpr modint inv() const noexcept {
        return pow(mod - 2);
    }
    friend std::ostream& operator<<(std::ostream& os, const modint& X) noexcept {
        return os << X.a;
    }
    friend std::istream& operator>>(std::istream& is, modint& X) noexcept {
        is >> X.a;
        X.a %= mod;
        if (X.a < 0) X.a += mod;
        return is;
    }
};

const int P = 26;
using mint = modint<998244353>;

void solve() {
    int N;
    cin >> N;
    string S;
    cin >> S;

    vector<vector<mint>> dp(P + 1, vector<mint>(P + 1, 0));
    dp[P][P] = 1;
    rep(i, N) {
        vector<vector<mint>> ndp(P + 1, vector<mint>(P + 1, 0));
        rep(j, P + 1) rep(k, P + 1) {
            if (dp[j][k] == 0) continue;

            if (S[i] == '?') {
                rep(d, P) {
                    if (d == j || d == k) continue;
                    ndp[k][d] += dp[j][k];
                }
            } else {
                if (S[i] - 'a' == j || S[i] - 'a' == k) continue;
                ndp[k][S[i] - 'a'] += dp[j][k];
            }
        }
        swap(dp, ndp);
    }

    mint ans = 0;
    rep(j, P + 1) rep(k, P + 1) ans += dp[j][k];

    cout << ans << endl;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0