結果
| 問題 | 
                            No.1646 Avoid Palindrome
                             | 
                    
| コンテスト | |
| ユーザー | 
                             hiro71687k
                         | 
                    
| 提出日時 | 2023-03-11 14:58:26 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,169 bytes | 
| コンパイル時間 | 3,543 ms | 
| コンパイル使用メモリ | 254,868 KB | 
| 最終ジャッジ日時 | 2025-02-11 09:59:44 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 13 TLE * 26 MLE * 1 | 
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
ld pie=3.141592653589793;
ll inf=6000000007999999999;
ll mod=998244353;
int main(){
    ll n;
    cin >> n;
    string s;
    cin >> s;
    for (ll i = 1; i < s.size(); i++)
    {
        if (s[i]!='?'&&s[i]==s[i-1])
        {
            cout << 0 << endl;
            return 0;
        }
        if (i>=2)
        {
            if (s[i]!='?'&&s[i]==s[i-2])
            {
                cout << 0 << endl;
                return 0;
            }
        }
    }
    ll ans=0;
    vector<vector<vector<ll>>>dp(n,vector<vector<ll>>(26,vector<ll>(26,0)));
    if (s[0]=='?')
    {
        for (ll i = 0; i < 26; i++)
        {
            dp[0][i][i]=1;
        }
    }else{
        dp[0][s[0]-'a'][s[0]-'a']=1;
    }
    for (ll i = 1; i < s.size(); i++)
    {
        if (s[i]!='?')
        {
            for (ll j = 0; j < 26; j++)
            {
                if (j==s[i]-'a')
                {
                    continue;
                }
                for (ll k = 0; k < 26; k++)
                {
                    if (k==s[i]-'a')
                    {
                        continue;
                    }
                    dp[i][s[i]-'a'][j]+=dp[i-1][j][k];
                    dp[i][s[i]-'a'][j]%=mod;
                }
            }
        }else{
            for (ll l = 0; l < 26; l++)
            {
                for (ll j = 0; j < 26; j++)
                {
                    if (j==l)
                    {
                        continue;
                    }
                    for (ll k = 0; k < 26; k++)
                    {
                        if (k==l)
                        {
                            continue;
                        }
                        dp[i][l][j]+=dp[i-1][j][k];
                        dp[i][l][j]%=mod;
                    }
                }
            }
        }
    }
    for (ll i = 0; i < 26; i++)
    {
        for (ll j = 0; j < 26; j++)
        {
            ans+=dp[n-1][i][j];
            ans%=mod;
        }
    }
    cout << ans << endl;
}
            
            
            
        
            
hiro71687k