結果

問題 No.1646 Avoid Palindrome
ユーザー umezo
提出日時 2021-08-13 22:56:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,214 ms / 3,000 ms
コード長 1,320 bytes
コンパイル時間 1,969 ms
コンパイル使用メモリ 197,288 KB
最終ジャッジ日時 2025-01-23 20:44:22
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

const int MOD=998244353;
ll dp[50050][26][26];

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n;
  string s;
  cin>>n>>s;
  s='$'+s;
  
  if(n==1){
    if(s[1]=='?') cout<<26<<endl;
    else cout<<1<<endl;
    return 0;
  }
  
  int a=s[1]-'a',b=s[2]-'a';
  if(a>=0 && a<26 && b>=0 && b<26){
    if(a!=b) dp[2][a][b]=1;
  }
  else if(a>=0 && a<26){
    rep(i,26){
      if(i!=a) dp[2][a][i]=1;
    }
  }
  else if(b>=0 && b<26){
    rep(i,26){
      if(i!=b) dp[2][i][b]=1;
    }
  }
  else{
    rep(i,26){
      rep(j,26){
        if(i==j) continue;
        dp[2][i][j]=1;
      }
    }
  }
  
  for(int i=3;i<=n;i++){
    int c=s[i]-'a';
    if(c>=0 && c<26){
      rep(j,26){
        if(j==c) continue;
        rep(k,26){
          if(k==c) continue;
          dp[i][k][c]=(dp[i][k][c]+dp[i-1][j][k])%MOD;
        }
      }
    }
    else{
      rep(j,26){
        rep(k,26){
          rep(l,26){
            if(l==j || l==k) continue;
            dp[i][k][l]=(dp[i][k][l]+dp[i-1][j][k])%MOD;
          }
        }
      }
    }
  }
  ll ans=0;
  rep(i,26){
    rep(j,26) ans=(ans+dp[n][i][j])%MOD;
  }
  cout<<ans<<endl;
  
  return 0;
}
0