結果

問題 No.1646 Avoid Palindrome
ユーザー monnumonnu
提出日時 2021-08-29 20:26:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 425 ms / 3,000 ms
コード長 1,880 bytes
コンパイル時間 3,963 ms
コンパイル使用メモリ 231,792 KB
実行使用メモリ 269,440 KB
最終ジャッジ日時 2024-05-01 23:52:49
合計ジャッジ時間 17,562 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 371 ms
254,080 KB
testcase_05 AC 369 ms
253,952 KB
testcase_06 AC 361 ms
245,504 KB
testcase_07 AC 373 ms
254,080 KB
testcase_08 AC 374 ms
256,384 KB
testcase_09 AC 359 ms
243,840 KB
testcase_10 AC 373 ms
249,984 KB
testcase_11 AC 354 ms
243,072 KB
testcase_12 AC 372 ms
253,440 KB
testcase_13 AC 371 ms
256,512 KB
testcase_14 AC 391 ms
249,216 KB
testcase_15 AC 397 ms
254,080 KB
testcase_16 AC 385 ms
247,680 KB
testcase_17 AC 403 ms
258,432 KB
testcase_18 AC 394 ms
251,648 KB
testcase_19 AC 397 ms
250,240 KB
testcase_20 AC 394 ms
252,288 KB
testcase_21 AC 407 ms
258,176 KB
testcase_22 AC 388 ms
249,472 KB
testcase_23 AC 405 ms
259,584 KB
testcase_24 AC 425 ms
269,440 KB
testcase_25 AC 421 ms
269,312 KB
testcase_26 AC 420 ms
269,440 KB
testcase_27 AC 420 ms
269,440 KB
testcase_28 AC 418 ms
269,440 KB
testcase_29 AC 368 ms
269,312 KB
testcase_30 AC 369 ms
269,440 KB
testcase_31 AC 369 ms
269,312 KB
testcase_32 AC 368 ms
269,440 KB
testcase_33 AC 367 ms
269,312 KB
testcase_34 AC 422 ms
269,440 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 369 ms
269,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll=long long;
#define MOD 998244353
using Graph=vector<vector<int>>;
#define INF 1000000000
#define MAX 200000

int main(){
  int N;
  string S;
  cin>>N>>S;
  if(N==1){
    if(S=="?"){
      cout<<26<<'\n';
    }else{
      cout<<1<<'\n';
    }
    return 0;
  }

  vector<vector<ll>> dp(N,vector<ll>(26*26,0));
  if(S[0]=='?'&&S[1]=='?'){
    for(int a=0;a<26;a++){
      for(int b=0;b<26;b++){
        if(a==b){
          continue;
        }
        dp[1][26*a+b]=1;
      }
    }
  }else if(S[0]=='?'){
    for(int a=0;a<26;a++){
      if(a==S[1]-'a'){
        continue;
      }
      dp[1][26*a+S[1]-'a']=1;
    }
  }else if(S[1]=='?'){
    for(int b=0;b<26;b++){
      if(S[0]-'a'==b){
        continue;
      }
      dp[1][26*(S[0]-'a')+b]=1;
    }
  }else{
    if(S[0]!=S[1]){
      dp[1][26*(S[0]-'a')+S[1]-'a']=1;
    }
  }
  for(int i=2;i<N;i++){
    if(S[i]!='?'){
      for(int a=0;a<26;a++){
        if(a==S[i]-'a'){
          continue;
        }
        for(int b=0;b<26;b++){
          if(b==S[i]-'a'){
            continue;
          }
          if(a==b){
            continue;
          }
          dp[i][26*b+(S[i]-'a')]+=dp[i-1][26*a+b];
          dp[i][26*b+(S[i]-'a')]%=MOD;
        }
      }
    }else{
      for(int b=0;b<26;b++){
        ll sum=0;
        for(int a=0;a<26;a++){
          sum+=dp[i-1][26*a+b];
          sum%=MOD;
        }
        for(int c=0;c<26;c++){
          if(b==c){
            continue;
          }
          dp[i][26*b+c]=sum;
        }
        for(int a=0;a<26;a++){
          if(a==b){
            continue;
          }
          dp[i][26*b+a]+=MOD-dp[i-1][26*a+b];
          dp[i][26*b+a]%=MOD;
        }
      }
    }
  }

  ll ans=0;
  for(int j=0;j<26*26;j++){
    ans+=dp[N-1][j];
    ans%=MOD;
  }
  cout<<ans<<'\n';
}
0