結果
| 問題 | 
                            No.1646 Avoid Palindrome
                             | 
                    
| コンテスト | |
| ユーザー | 
                             auaua
                         | 
                    
| 提出日時 | 2021-08-13 21:45:27 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2,622 ms / 3,000 ms | 
| コード長 | 1,961 bytes | 
| コンパイル時間 | 1,841 ms | 
| コンパイル使用メモリ | 174,748 KB | 
| 実行使用メモリ | 320,128 KB | 
| 最終ジャッジ日時 | 2024-11-08 15:08:14 | 
| 合計ジャッジ時間 | 62,134 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 40 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
//#define vec vector<ll>
//#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=INF*INF;
const ll mod=MOD;
const ll MAX=200010;
const double PI=acos(-1.0);
typedef vector<vector<ll> > mat;
typedef vector<ll> vec;
signed main(){
    ll n;
    string s;cin>>n>>s;
    if(n==1){
        cout<<(s[0]=='?'?26:1)<<endl;
        return 0;
    }
    vector<vector<vector<ll> > >dp(n+1,vector<vector<ll> >(26,vector<ll>(26)));
    if(s[0]=='?'){
        if(s[1]=='?'){
            rep(i,26){
                rep(j,26){
                    if(i!=j)dp[2][i][j]=1;
                }
            }
        }else{
            rep(i,26){
                if(i!=s[1]-'a')dp[2][i][s[1]-'a']=1;
            }
        }
    }else{
        if(s[1]=='?'){
            rep(i,26){
                if(i!=s[0]-'a')dp[2][s[0]-'a'][i]=1;
            }
        }else{
            if(s[0]!=s[1])dp[2][s[0]-'a'][s[1]-'a']=1;
        }
    }
    REP(i,2,n){
        rep(j,26){
            rep(k,26){
                if(s[i]=='?'){
                    rep(l,26){
                        if(k!=l&&j!=l)dp[i+1][k][l]=(dp[i+1][k][l]+dp[i][j][k])%mod;
                    }
                }else{
                    ll l=s[i]-'a';
                    if(k!=l&&j!=l)dp[i+1][k][l]=(dp[i+1][k][l]+dp[i][j][k])%mod;
                }
            }
        }
    }
    ll ans=0;
    rep(i,26){
        rep(j,26){
            ans=(ans+dp[n][i][j])%mod;
        }
    }
    cout<<ans<<endl;
}
            
            
            
        
            
auaua