結果

問題 No.1646 Avoid Palindrome
ユーザー HIcoder
提出日時 2023-07-22 18:08:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 2,790 bytes
コンパイル時間 1,082 ms
コンパイル使用メモリ 105,616 KB
最終ジャッジ日時 2025-02-15 18:06:47
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 22 WA * 2 TLE * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<stack>
#include<numeric>
#include<queue>
#include<cmath>
#include<deque>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<int,int> P;
typedef pair<ll,P> PP;
const ll MOD=998244353;


int main(){
    int N;
    cin>>N;
    string S;
    cin>>S;

    /*
    dp[i][j][k]=i文字目がk, i-1文字目がj
    */

    vector dp(N+1,vector<vector<ll>>(26,vector<ll>(26,0)));

    for(int j=0;j<26;j++){

        if( !(S[0]-'a' == j || S[0]=='?') )continue;

        for(int k=0;k<26;k++){
            if( !(S[1]-'a'==k || S[1]=='?') || k==j ) continue;

            dp[1][j][k]=1;
        }
    }

    for(int i=2;i<N;i++){

        for(int j=0;j<26;j++){
            for(int k=0;k<26;k++){

                if(j==k) continue;

                for(int l=0;l<26;l++){
                    // j,k,l となっている. i文字目がl文字目
                    if(l==k || l==j) continue;

                    if(!(S[i]-'a'==l || S[i]=='?')){
                        continue;
                    }

                    dp[i][k][l]+=dp[i-1][j][k];
                    dp[i][k][l]%=MOD;

                }



            }
        }

    }

    ll ans=0;

    for(int j=0;j<26;j++){
        for(int k=0;k<26;k++){
            if(j==k) continue;
            ans+=dp[N-1][j][k];
            ans%=MOD;
        }
    }
    cout<<ans<<endl;


    /*
    for(int i=0;i<26;i++){
        for(int j=0;j<26;j++){
            if(i!=j){
                dp[0][i][j]=1;
            }
        }
    }

    S=S+ (S.back()-'a'+1)%26
    


    for(int i=0;i<N;i++){
        if(S[i]!='?'){

            for(int j=0;j<26;j++){
                for(int k=0;k<26;k++){
                    if(j==k) continue;

                    if(k==(S[i]-'a'))continue;  //k==S[i]となるとまずい
                    if(j==(S[i]-'a'))continue;  //j==S[i]となるとまずい

            
                    dp[i+1][k][S[i]-'a']+=dp[i][j][k];
                
                }
            }

        }else{
            //S[i]=='?'

            for(int j=0;j<26;j++){
                for(int k=0;k<26;k++){

                    for(int l=0;l<26;l++){
                        if(j==k) continue;
                        if(j==l) continue;
                        if(k==l) continue;
                    

                        dp[i+1][k][l]+=dp[i][j][k];

                    }

                }
            }


        }
    }

    for(int j=0;j<26;j++){
        for(int k=0;k<26;k++){
        cout<<"dp["<<j<<"]["<<k<<"]="<<dp[N][j][k]<<endl;
        }
    }

    if(N>=2){
        ll ans=dp[N][S[S.size()-2]-'a'][S[S.size()-1]-'a'];
        cout<<ans<<endl;
    }
    */
}
0