結果

問題 No.1646 Avoid Palindrome
ユーザー monnumonnu
提出日時 2021-08-29 20:24:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,866 bytes
コンパイル時間 5,117 ms
コンパイル使用メモリ 231,832 KB
実行使用メモリ 269,440 KB
最終ジャッジ日時 2024-05-01 23:51:28
合計ジャッジ時間 18,145 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 373 ms
254,080 KB
testcase_05 AC 372 ms
254,080 KB
testcase_06 AC 357 ms
245,504 KB
testcase_07 AC 369 ms
254,080 KB
testcase_08 AC 375 ms
256,512 KB
testcase_09 AC 355 ms
243,840 KB
testcase_10 AC 366 ms
249,984 KB
testcase_11 AC 357 ms
243,072 KB
testcase_12 AC 372 ms
253,440 KB
testcase_13 AC 379 ms
256,512 KB
testcase_14 AC 394 ms
249,216 KB
testcase_15 AC 401 ms
254,080 KB
testcase_16 AC 389 ms
247,552 KB
testcase_17 AC 407 ms
258,432 KB
testcase_18 AC 399 ms
251,520 KB
testcase_19 AC 393 ms
250,368 KB
testcase_20 AC 401 ms
252,288 KB
testcase_21 AC 413 ms
258,176 KB
testcase_22 AC 398 ms
249,472 KB
testcase_23 AC 413 ms
259,456 KB
testcase_24 AC 425 ms
269,440 KB
testcase_25 AC 429 ms
269,312 KB
testcase_26 AC 426 ms
269,440 KB
testcase_27 AC 433 ms
269,440 KB
testcase_28 AC 424 ms
269,440 KB
testcase_29 AC 368 ms
269,440 KB
testcase_30 AC 372 ms
269,312 KB
testcase_31 AC 369 ms
269,312 KB
testcase_32 AC 371 ms
269,440 KB
testcase_33 AC 366 ms
269,440 KB
testcase_34 AC 427 ms
269,312 KB
testcase_35 RE -
testcase_36 RE -
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,944 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 369 ms
269,312 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<<0<<'\n';
    }
  }

  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