結果

問題 No.1646 Avoid Palindrome
ユーザー hiro71687khiro71687k
提出日時 2023-03-11 14:58:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,169 bytes
コンパイル時間 4,486 ms
コンパイル使用メモリ 267,696 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-10-18 09:53:37
合計ジャッジ時間 10,602 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 7 ms
4,348 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0