結果

問題 No.1646 Avoid Palindrome
ユーザー rianoriano
提出日時 2021-08-13 21:56:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,732 ms / 3,000 ms
コード長 3,960 bytes
コンパイル時間 1,508 ms
コンパイル使用メモリ 168,312 KB
実行使用メモリ 267,812 KB
最終ジャッジ日時 2023-08-08 09:39:06
合計ジャッジ時間 35,764 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 413 ms
252,292 KB
testcase_05 AC 406 ms
252,356 KB
testcase_06 AC 385 ms
243,828 KB
testcase_07 AC 395 ms
252,388 KB
testcase_08 AC 399 ms
254,696 KB
testcase_09 AC 380 ms
242,212 KB
testcase_10 AC 391 ms
248,256 KB
testcase_11 AC 377 ms
241,448 KB
testcase_12 AC 400 ms
251,796 KB
testcase_13 AC 398 ms
254,884 KB
testcase_14 AC 1,596 ms
247,404 KB
testcase_15 AC 1,630 ms
252,600 KB
testcase_16 AC 1,588 ms
245,940 KB
testcase_17 AC 1,658 ms
256,636 KB
testcase_18 AC 1,609 ms
249,780 KB
testcase_19 AC 1,603 ms
248,684 KB
testcase_20 AC 1,614 ms
250,568 KB
testcase_21 AC 1,657 ms
256,448 KB
testcase_22 AC 1,597 ms
247,672 KB
testcase_23 AC 1,660 ms
257,824 KB
testcase_24 AC 1,732 ms
267,812 KB
testcase_25 AC 1,728 ms
267,484 KB
testcase_26 AC 1,730 ms
267,488 KB
testcase_27 AC 1,729 ms
267,688 KB
testcase_28 AC 1,730 ms
267,636 KB
testcase_29 AC 250 ms
267,708 KB
testcase_30 AC 250 ms
267,468 KB
testcase_31 AC 251 ms
267,476 KB
testcase_32 AC 253 ms
267,484 KB
testcase_33 AC 252 ms
267,700 KB
testcase_34 AC 1,729 ms
267,544 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 1 ms
4,384 KB
testcase_43 AC 251 ms
267,544 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