結果

問題 No.1646 Avoid Palindrome
ユーザー nawawannawawan
提出日時 2021-08-13 21:58:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 483 ms / 3,000 ms
コード長 4,046 bytes
コンパイル時間 2,340 ms
コンパイル使用メモリ 203,128 KB
実行使用メモリ 320,256 KB
最終ジャッジ日時 2024-04-26 02:53:41
合計ジャッジ時間 17,705 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 413 ms
301,824 KB
testcase_05 AC 415 ms
301,952 KB
testcase_06 AC 398 ms
291,584 KB
testcase_07 AC 418 ms
301,952 KB
testcase_08 AC 419 ms
304,640 KB
testcase_09 AC 399 ms
289,664 KB
testcase_10 AC 409 ms
296,960 KB
testcase_11 AC 394 ms
288,768 KB
testcase_12 AC 416 ms
301,312 KB
testcase_13 AC 414 ms
304,768 KB
testcase_14 AC 446 ms
296,064 KB
testcase_15 AC 452 ms
301,824 KB
testcase_16 AC 441 ms
294,272 KB
testcase_17 AC 463 ms
306,944 KB
testcase_18 AC 446 ms
298,880 KB
testcase_19 AC 449 ms
297,344 KB
testcase_20 AC 448 ms
299,776 KB
testcase_21 AC 461 ms
306,816 KB
testcase_22 AC 445 ms
296,448 KB
testcase_23 AC 467 ms
308,480 KB
testcase_24 AC 476 ms
320,128 KB
testcase_25 AC 481 ms
320,000 KB
testcase_26 AC 477 ms
320,256 KB
testcase_27 AC 480 ms
320,128 KB
testcase_28 AC 481 ms
320,128 KB
testcase_29 AC 413 ms
320,128 KB
testcase_30 AC 418 ms
320,128 KB
testcase_31 AC 414 ms
320,256 KB
testcase_32 AC 419 ms
320,256 KB
testcase_33 AC 415 ms
320,256 KB
testcase_34 AC 483 ms
320,128 KB
testcase_35 AC 2 ms
6,944 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 1 ms
6,940 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 416 ms
320,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<const int MOD> struct modint{
    long long val;
    modint(): val(0) {}
    modint(long long x){
        if(x < 0) val = x % MOD + MOD;
        else val = x % MOD;
    }
    modint(const modint &t){
        val = t.val;
    }
    modint& operator =(const modint m){
        val = m.val;
        return *this;
    }
    modint operator -(){
        return modint(-val);
    }
    modint& operator-=(const modint &m){
        val -= m.val;
        if(val < 0) val += MOD;
        return *this;
    }
    modint& operator+=(const modint &m){
        val += m.val;
        if(val >= MOD) val -= MOD;
        return *this;
    }
    modint& operator*=(const modint &m){
        val *= m.val;
        val %= MOD;
        return *this;
    }
    modint& operator/=(modint m){
        *this *= m.inv();
        return *this;
    }
    modint inv(){
        long long x = 1, y = 0;
        long long a = val, b = MOD;
        while(b != 0){
            long long t = a / b;
            a -= t * b;
            x -= t * y;
            swap(a, b);
            swap(x, y);
        }
        x %= MOD;
        if(x < 0) x += MOD;
        return modint(x);
    }
    modint pow(long long k){
        long long res = 1;
        long long v = val;
        while(k > 0){
            if(k & 1) res = res * v % MOD;
            v = v * v % MOD;
            k >>= 1;
        }
        return modint(res);
    }
    bool operator==(const modint &m){
        return val == m.val;
    }
    modint operator+(const modint &m){
        return modint(*this) += m;
    }
    modint operator-(const modint &m){
        return modint(*this) -= m;
    }
    modint operator*(const modint &m){
        return modint(*this) *= m;
    }
    modint operator/(const modint &m){
        return modint(*this) /= m;
    }
    bool operator!=(const modint &m){
        return modint(*this).val != m.val;
    }
    bool operator!=(const int &m){
        return modint(*this).val != m;
    }
};
const int MOD = 998244353;
using mint = modint<MOD>;
int main(){
    int N;
    cin >> N;
    string S;
    cin >> S;
    if(N == 1){
        if(S[0] == '?') cout << 26 << endl;
        else cout << 1 << endl;
        return 0;
    }
    vector<vector<vector<mint>>> dp(N, vector<vector<mint>>(26, vector<mint>(26)));
    if(S[0] == '?'){
        if(S[1] == '?'){
            for(int i = 0; i < 26; i++){
                for(int j = 0; j < 26; j++){
                    if(i == j) continue;
                    dp[1][i][j] = 1;
                }
            }
        }
        else{
            for(int i = 0; i < 26; i++){
                if(i == S[1] - 'a') continue;
                dp[1][S[1] - 'a'][i] = 1;
            }
        }
    }
    else{
        if(S[1] == '?'){
            for(int i = 0; i < 26; i++){
                if(i == S[0] - 'a') continue;
                dp[1][i][S[0] - 'a'] = 1;
            }
        }
        else{
            if(S[1] != S[0]) dp[1][S[1] - 'a'][S[0] - 'a'] = 1;
        }
    }
    for(int i = 2; i < N; i++){
        if(S[i] == '?'){
            for(int j = 0; j < 26; j++){
                mint temp = 0;
                for(int k = 0; k < 26; k++) {
                    if(j == k) continue;
                    temp += dp[i - 1][j][k];
                }
                for(int k = 0; k < 26; k++){
                    if(j == k) continue;
                    dp[i][k][j] += temp - dp[i - 1][j][k];
                }
            }
        }
        else{
            int id = S[i] - 'a';
            for(int j = 0; j < 26; j++){
                if(id == j) continue;
                mint temp = 0;
                for(int k = 0; k < 26; k++){
                    if(k == id) continue;
                    temp += dp[i - 1][j][k];
                }
                dp[i][id][j] += temp;
            }
        }
    }
    mint ans = 0;
    for(int i = 0; i < 26; i++){
        for(int j = 0; j < 26; j++){
            ans += dp[N - 1][i][j];
        }
    }
    cout << ans.val << endl;
}
0