結果

問題 No.1646 Avoid Palindrome
ユーザー HIcoderHIcoder
提出日時 2023-07-22 18:29:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 597 ms / 3,000 ms
コード長 2,151 bytes
コンパイル時間 1,269 ms
コンパイル使用メモリ 109,632 KB
実行使用メモリ 320,188 KB
最終ジャッジ日時 2023-10-24 01:01:50
合計ジャッジ時間 19,471 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 507 ms
301,972 KB
testcase_05 AC 499 ms
301,972 KB
testcase_06 AC 481 ms
291,676 KB
testcase_07 AC 497 ms
301,972 KB
testcase_08 AC 502 ms
304,876 KB
testcase_09 AC 475 ms
289,828 KB
testcase_10 AC 484 ms
296,956 KB
testcase_11 AC 477 ms
288,772 KB
testcase_12 AC 488 ms
301,444 KB
testcase_13 AC 497 ms
304,876 KB
testcase_14 AC 527 ms
296,164 KB
testcase_15 AC 541 ms
301,972 KB
testcase_16 AC 525 ms
294,316 KB
testcase_17 AC 550 ms
307,252 KB
testcase_18 AC 534 ms
299,068 KB
testcase_19 AC 535 ms
297,484 KB
testcase_20 AC 538 ms
299,860 KB
testcase_21 AC 549 ms
306,988 KB
testcase_22 AC 532 ms
296,428 KB
testcase_23 AC 554 ms
308,572 KB
testcase_24 AC 574 ms
320,188 KB
testcase_25 AC 573 ms
320,188 KB
testcase_26 AC 573 ms
320,188 KB
testcase_27 AC 597 ms
320,188 KB
testcase_28 AC 569 ms
320,188 KB
testcase_29 AC 490 ms
320,188 KB
testcase_30 AC 488 ms
320,188 KB
testcase_31 AC 495 ms
320,188 KB
testcase_32 AC 493 ms
320,188 KB
testcase_33 AC 495 ms
320,188 KB
testcase_34 AC 573 ms
320,188 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 493 ms
320,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
O(N*σ^3)でTLE

*/
#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;

    if(N==1){
        cout<<(S[0]=='?'?26:1)<<endl;
        return 0;
    }

  

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

    vector<ll> sum(26,0);

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

        if( S[0]-'a' == j || S[0]=='?' ){

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

                    dp[1][j][k]=1;
                    sum[k]+=1;
                    sum[k]%=MOD;

                }
            }
        }
    }

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

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

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

            if(S[i]!='?'){
                if(S[i]-'a'!=k){
                    //i=2のとき, i=0文字目がS[i]-'a'だった時を引けば良い
                    dp[i][k][S[i]-'a']+=(sum[k]-dp[i-1][S[i]-'a'][k]+MOD)%MOD;
                    dp[i][k][S[i]-'a']%=MOD;
                }
            }else{

                for(int l=0;l<26;l++){
                    //直前の文字列は順番に, j,k,l となっている. i文字目がl文字目
                    if(k!=l){
                        dp[i][k][l]+=(sum[k]-dp[i-1][l][k]+MOD)%MOD;
                        dp[i][k][l]%=MOD;
                    }                

                }


            }

           
        }

        for(int k=0;k<26;k++){
            sum[k]=0;
            for(int j=0;j<26;j++){
                sum[k]+=dp[i][j][k];
                sum[k]%=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;


}
0