結果

問題 No.1646 Avoid Palindrome
ユーザー yunix91201367yunix91201367
提出日時 2021-08-13 22:13:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,895 bytes
コンパイル時間 1,891 ms
コンパイル使用メモリ 176,312 KB
実行使用メモリ 302,080 KB
最終ジャッジ日時 2024-04-14 18:22:11
合計ジャッジ時間 6,694 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,756 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 5 ms
6,944 KB
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/math>
using namespace std;
using namespace atcoder;
#define int long long
using vec_int = vector<int>;
using vec_ii = vector<vec_int>;
using vec_iii = vector<vec_ii>;
using vec_iiii = vector<vec_iii>;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using ll = long long;
using ld = long double;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

void cout_line(vector<int> &a){
    for(int i=0;i<a.size();i++){
        if(i<a.size()-1){
            cout<<a.at(i)<<" ";
        }else{
            cout<<a.at(i)<<endl;
        }
    }
}

int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}

signed main(){
    int N; cin>>N;
    string S; cin>>S;
    int MOD = 998244353;

    if(N==1&&S=="?"){
        cout<<26<<endl;
        return 0;
    }else if(N==1){
        cout<<1<<endl;
        return 0;
    }

    if(N==2){
        if(S=="??"){
            cout<<26*25<<endl;
            return 0;
        }
        if(S.at(0)=='?'||S.at(1)=='?'){
            cout<<25<<endl;
            return 0;
        }else{
            if(S.at(0)!=S.at(1)){
                cout<<1<<endl;
                return 0;
            }else{
                cout<<0<<endl;
                return 0;
            }
        }
    }

    rep(i, S.size()-1){
        if(S.at(i)!='?'&&S.at(i+1)!='?'){
            if(S.at(i)==S.at(i+1)){
                cout<<0<<endl;
                return 0;
            }
        }
    }
    rep(i, S.size()-2){
        if(S.at(i)!='?'&&S.at(i+2)!='?'){
            if(S.at(i)==S.at(i+2)){
                cout<<0<<endl;
                return 0;
            }
        }
    }

    vec_iii DP(N-1, vec_ii(26, vec_int(26, 0)));
    rep(i, 26)rep(j, 26){
        // if(S.at(0)=='?')continue;
        // if(S.at(1)=='?')continue;
        if(S.at(0)!='?'&&(int)(S.at(0)-'a')!=i)continue;
        if(S.at(1)!='?'&&(int)(S.at(1)-'a')!=j)continue;
        if(i==j)continue;
        DP.at(0).at(i).at(j) = 1;
    }

    for(int i=1;i<N-1;i++){
        int num = (int)(S.at(i+1)-'a');
        rep(ii, 26)rep(j, 26)rep(k,26){
            if(S.at(i+1)!='?'&&ii!=num)continue;
            if(ii==j||ii==k)continue;
            DP.at(i).at(j).at(ii) += DP.at(i-1).at(k).at(j);
            DP.at(i).at(j).at(ii) %= MOD;
            /*
            if(S.at(i)!='?'&&(int)(S.at(i)-'a')!=j)continue;
            if(S.at(i+1)!='?'&&(int)(S.at(i+1)-'a')!=k)continue;
            if(j==k)continue;
            if(ii==j)continue;
            if(ii==k)continue;
            // cout<<i<<" "<<ii<<" "<<j<<" "<<k<<" "<<DP.at(i-1).at(j).at(k)<<endl;
            DP.at(i).at(j).at(ii) += DP.at(i-1).at(k).at(j);
            DP.at(i).at(j).at(ii) %= MOD;
            */
        }
    }
    int ans = 0;
    rep(i, 26) rep(j, 26){
        ans += DP.at(N-2).at(i).at(j);
        ans %= MOD;
    }
    cout<<ans<<endl;



    return 0;
}
0