結果

問題 No.1646 Avoid Palindrome
ユーザー sotanishysotanishy
提出日時 2021-08-14 23:06:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,095 ms / 3,000 ms
コード長 3,288 bytes
コンパイル時間 2,395 ms
コンパイル使用メモリ 199,328 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-26 03:49:46
合計ジャッジ時間 26,535 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,812 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 517 ms
6,940 KB
testcase_05 AC 513 ms
6,944 KB
testcase_06 AC 496 ms
6,944 KB
testcase_07 AC 515 ms
6,948 KB
testcase_08 AC 518 ms
6,940 KB
testcase_09 AC 486 ms
6,940 KB
testcase_10 AC 507 ms
6,940 KB
testcase_11 AC 493 ms
6,940 KB
testcase_12 AC 518 ms
6,944 KB
testcase_13 AC 519 ms
6,940 KB
testcase_14 AC 1,009 ms
6,944 KB
testcase_15 AC 1,013 ms
6,944 KB
testcase_16 AC 995 ms
6,944 KB
testcase_17 AC 1,038 ms
6,940 KB
testcase_18 AC 1,010 ms
6,940 KB
testcase_19 AC 1,004 ms
6,944 KB
testcase_20 AC 1,010 ms
6,944 KB
testcase_21 AC 1,045 ms
6,940 KB
testcase_22 AC 1,008 ms
6,940 KB
testcase_23 AC 1,045 ms
6,940 KB
testcase_24 AC 1,065 ms
6,944 KB
testcase_25 AC 1,076 ms
6,944 KB
testcase_26 AC 1,080 ms
6,940 KB
testcase_27 AC 1,081 ms
6,944 KB
testcase_28 AC 1,086 ms
6,940 KB
testcase_29 AC 188 ms
6,944 KB
testcase_30 AC 188 ms
6,940 KB
testcase_31 AC 186 ms
6,944 KB
testcase_32 AC 189 ms
6,940 KB
testcase_33 AC 190 ms
6,940 KB
testcase_34 AC 1,095 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,944 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 189 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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