結果

問題 No.1646 Avoid Palindrome
ユーザー tabae326tabae326
提出日時 2021-08-13 23:11:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,127 ms / 3,000 ms
コード長 2,268 bytes
コンパイル時間 2,413 ms
コンパイル使用メモリ 202,372 KB
実行使用メモリ 135,832 KB
最終ジャッジ日時 2024-04-26 03:32:14
合計ジャッジ時間 50,311 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
135,636 KB
testcase_01 AC 61 ms
135,640 KB
testcase_02 AC 62 ms
135,544 KB
testcase_03 AC 62 ms
135,492 KB
testcase_04 AC 1,069 ms
135,644 KB
testcase_05 AC 1,035 ms
135,600 KB
testcase_06 AC 1,009 ms
135,644 KB
testcase_07 AC 1,039 ms
135,600 KB
testcase_08 AC 1,045 ms
135,672 KB
testcase_09 AC 992 ms
135,704 KB
testcase_10 AC 1,009 ms
135,688 KB
testcase_11 AC 995 ms
135,608 KB
testcase_12 AC 1,039 ms
135,756 KB
testcase_13 AC 1,050 ms
135,608 KB
testcase_14 AC 1,954 ms
135,740 KB
testcase_15 AC 1,992 ms
135,716 KB
testcase_16 AC 1,949 ms
135,624 KB
testcase_17 AC 2,016 ms
135,832 KB
testcase_18 AC 1,970 ms
135,544 KB
testcase_19 AC 1,957 ms
135,636 KB
testcase_20 AC 1,977 ms
135,672 KB
testcase_21 AC 2,015 ms
135,656 KB
testcase_22 AC 1,960 ms
135,608 KB
testcase_23 AC 2,030 ms
135,780 KB
testcase_24 AC 2,115 ms
135,692 KB
testcase_25 AC 2,115 ms
135,640 KB
testcase_26 AC 2,127 ms
135,624 KB
testcase_27 AC 2,112 ms
135,828 KB
testcase_28 AC 2,103 ms
135,572 KB
testcase_29 AC 430 ms
135,716 KB
testcase_30 AC 427 ms
135,612 KB
testcase_31 AC 423 ms
135,496 KB
testcase_32 AC 430 ms
135,740 KB
testcase_33 AC 430 ms
135,612 KB
testcase_34 AC 2,107 ms
135,816 KB
testcase_35 AC 60 ms
135,592 KB
testcase_36 AC 60 ms
135,536 KB
testcase_37 AC 60 ms
135,576 KB
testcase_38 AC 60 ms
135,636 KB
testcase_39 AC 60 ms
135,660 KB
testcase_40 AC 61 ms
135,508 KB
testcase_41 AC 59 ms
135,716 KB
testcase_42 AC 60 ms
135,692 KB
testcase_43 AC 428 ms
135,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)

// For debug
// Ref: https://qiita.com/ysuzuki19/items/d89057d65284ba1a16ac
#define dump(var)  do{std::cerr << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cerr << e << "\n";}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << "\n";}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cerr << "\n"; for(const auto& v : vv){ view(v); } }
template<typename T> void dump_cout(const T& v) { for(long long i = 0; i < v.size(); i++) std::cout << v[i] << (i == v.size()-1 ? "\n" : " "); }

#include <atcoder/modint>
using mint = atcoder::modint998244353;

#define N 50010
mint dp[N][26][26];

void solve() {
    ll n;
    string s;
    cin >> n >> s;
    if(n == 1) {
        cout << (s[0] == '?' ? 26 : 1) << endl;
        return;
    }
    bool f = true;
    for(auto e : s) if(e == '?') f = false;
    if(f) {
        cout << (s == string(s.rbegin(), s.rend()) ? 0 : 1) << endl;
        return;
    }
    rep(x, 0, 26) {
        if(s[0] != '?' && s[0] != 'a' + x) continue; 
        rep(y, 0, 26) {
           if(s[1] != '?' && s[1] != 'a' + y) continue;
           if(x == y) continue;
           dp[2][x][y] = 1; 
        }
    }
    rep(i, 2, n) {
        if(s[i] != '?') {
            rep(x, 0, 26) {
                if(s[i] == 'a' + x) continue;
                rep(y, 0, 26) {
                    if(s[i] == 'a' + y || x == y) continue;
                    dp[i+1][y][s[i]-'a'] += dp[i][x][y];
                }
            }        
        } else {
            rep(k, 0, 26) {
                rep(x, 0, 26) {
                    if(k == x) continue;
                    rep(y, 0, 26) {
                        if(k == y || x == y) continue;
                        dp[i+1][y][k] += dp[i][x][y];
                    }
                }   
            }
        }
    }
    mint ans = 0;
    rep(x, 0, 26) rep(y, 0, 26) {
        if(x!=y) ans += dp[n][x][y];
    }
    cout << ans.val() << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
0