結果

問題 No.1646 Avoid Palindrome
ユーザー rianoriano
提出日時 2021-08-13 21:56:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,548 ms / 3,000 ms
コード長 3,960 bytes
コンパイル時間 1,938 ms
コンパイル使用メモリ 167,900 KB
実行使用メモリ 267,756 KB
最終ジャッジ日時 2024-04-26 02:52:15
合計ジャッジ時間 48,038 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 507 ms
252,436 KB
testcase_05 AC 508 ms
252,456 KB
testcase_06 AC 484 ms
243,836 KB
testcase_07 AC 501 ms
252,408 KB
testcase_08 AC 506 ms
254,876 KB
testcase_09 AC 477 ms
242,376 KB
testcase_10 AC 482 ms
248,296 KB
testcase_11 AC 475 ms
241,580 KB
testcase_12 AC 503 ms
251,852 KB
testcase_13 AC 506 ms
254,792 KB
testcase_14 AC 2,278 ms
247,572 KB
testcase_15 AC 2,336 ms
252,452 KB
testcase_16 AC 2,263 ms
246,132 KB
testcase_17 AC 2,375 ms
256,720 KB
testcase_18 AC 2,321 ms
249,960 KB
testcase_19 AC 2,283 ms
248,792 KB
testcase_20 AC 2,283 ms
250,836 KB
testcase_21 AC 2,297 ms
256,376 KB
testcase_22 AC 2,239 ms
247,896 KB
testcase_23 AC 2,338 ms
257,924 KB
testcase_24 AC 2,446 ms
267,636 KB
testcase_25 AC 2,514 ms
267,724 KB
testcase_26 AC 2,497 ms
267,564 KB
testcase_27 AC 2,442 ms
267,640 KB
testcase_28 AC 2,548 ms
267,756 KB
testcase_29 AC 278 ms
267,660 KB
testcase_30 AC 279 ms
267,608 KB
testcase_31 AC 276 ms
267,504 KB
testcase_32 AC 280 ms
267,660 KB
testcase_33 AC 279 ms
267,740 KB
testcase_34 AC 2,416 ms
267,548 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 1 ms
6,944 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 1 ms
6,944 KB
testcase_39 AC 2 ms
6,948 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 1 ms
6,944 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 280 ms
267,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<int,int,int>
#define riano_ std::ios::sync_with_stdio(false);std::cin.tie(nullptr);typedef modint<mod> mint
using Graph = vector<vector<int>>;

const ll mod = 998244353;
template<uint64_t mod>
struct modint{
    uint64_t val;
    constexpr modint(const int64_t val_=0) noexcept:val((val_%int64_t(mod)+int64_t(mod))%int64_t(mod)){}
    constexpr modint operator-() const noexcept{
        return modint(*this)=mod-val;
    }
    constexpr modint operator+(const modint rhs) const noexcept{
        return modint(*this)+=rhs;
    }
    constexpr modint operator-(const modint rhs) const noexcept{
        return modint(*this)-=rhs;
    }
    constexpr modint operator*(const modint rhs) const noexcept{
        return modint(*this)*=rhs;
    }
    constexpr modint operator/(const modint rhs) const noexcept{
        return modint(*this)/=rhs;
    }
    constexpr modint &operator+=(const modint rhs) noexcept{
        val+=rhs.val;
        val-=((val>=mod)?mod:0);
        return (*this);
    }
    constexpr modint &operator-=(const modint rhs) noexcept{
        val+=((val<rhs.val)?mod:0);
        val-=rhs.val;
        return (*this);
    }
    constexpr modint &operator*=(const modint rhs) noexcept{
        val=val*rhs.val%mod;
        return (*this);
    }
    constexpr modint &operator/=(modint rhs) noexcept{
        uint64_t ex=mod-2;
        modint now=1;
        while(ex){
            now*=((ex&1)?rhs:1);
            rhs*=rhs,ex>>=1;
        }
        return (*this)*=now;
    }
    constexpr bool operator==(const modint rhs) noexcept{
        return val==rhs.val;
    }
    constexpr bool operator!=(const modint rhs) noexcept{
        return val!=rhs.val;
    }
    friend constexpr ostream &operator<<(ostream& os,const modint x) noexcept{
        return os<<(x.val);
    }
    friend constexpr istream &operator>>(istream& is,modint& x) noexcept{
        uint64_t t;
        is>>t,x=t;
        return is;
    }
};

int main() {
    riano_;
    //ll N; cin >> N; ll ans = 0;
    ll N; cin >> N;
    string S; cin >> S;
    bool ok = true;
    rep(i,N-1){
        if(S[i]==S[i+1]&&S[i]!='?') ok = false;
        if(i+2<N&&S[i]==S[i+2]&&S[i]!='?') ok = false;
    }
    if(!ok){
        cout << 0 << endl; return 0;
    }
    mint dp[N+1][26*26];
    rep(i,N+1){
        rep(j,26*26) dp[i][j] = 0;
    }
    rep(i,N){
        if(i==0){
            if(S[i]!='?'){
                dp[1][S[i]-'a'] = 1;
            }
            else{
                rep(j,26){
                    dp[1][j] = 1;
                }
            }
        }
        if(i==1){
            if(S[i]=='?'){
                rep(j,26){
                    if(dp[1][j]==0) continue;
                    rep(k,26){
                        if(j==k) continue;
                        dp[2][26*j+k] += dp[1][j];
                    }
                }
            }
            else{
                rep(j,26){
                    if(j==S[i]-'a') continue;
                    dp[2][S[i]-'a'+26*j] += dp[1][j];
                }
            }
        }
        else{
            if(S[i]=='?'){
                rep(j,26*26){
                    if(dp[i][j]==0) continue;
                    ll k1 = j%26; ll k2 = j/26;
                    rep(k,26){
                        if(k1==k||k2==k) continue;
                        dp[i+1][k1*26+k] += dp[i][j];
                    }
                }
            }
            else{
                rep(j,26*26){
                    if(dp[i][j]==0) continue;
                    ll k1 = j%26; ll k2 = j/26;
                    ll k = S[i]-'a';
                    if(k1==k||k2==k) continue;
                    dp[i+1][k1*26+k] += dp[i][j];
                }
            }
        }
    }
    mint ans = 0;
    rep(i,26*26){
        ans += dp[N][i];
    }
    cout << ans << endl;
}
0