結果

問題 No.1646 Avoid Palindrome
コンテスト
ユーザー lolk
提出日時 2021-08-18 20:54:44
言語 C++17(clang)
(clang++ 22.1.2 + boost 1.89.0)
コンパイル:
clang++ -O2 -lm -std=c++1z -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1,120 ms / 3,000 ms
コード長 1,567 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,352 ms
コンパイル使用メモリ 111,080 KB
実行使用メモリ 268,032 KB
最終ジャッジ日時 2026-05-08 18:49:33
合計ジャッジ時間 27,655 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:13:12: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   13 |     char s[n+1];
      |            ^~~
main.cpp:13:12: note: read of non-const variable 'n' is not allowed in a constant expression
main.cpp:10:9: note: declared here
   10 |     int n;
      |         ^
1 warning generated.

ソースコード

diff #
raw source code

#include <stdio.h>

#define ll long long
#define MOD 998244353
#define MAX 50005

ll dp[MAX][26][26];

int main(void) {
    int n;
    scanf("%d", &n);

    char s[n+1];
    scanf("%s", s);

    if (n == 1) {
        printf("%d\n", s[0] == '?' ? 26 : 1);
        return (0);
    }

    for (int i = 0; i < 26; ++i) {
        for (int j = 0; j < 26; ++j) {
            if (i == j) continue;
            if (s[0] != '?' && s[0] - 'a' != i) continue;
            if (s[1] != '?' && s[1] - 'a' != j) continue;

            ++dp[2][i][j];
        }
    }

    for (int i = 2; i < n; ++i) {
        if (s[i] == '?') {
            for (int a = 0; a < 26; ++a) {
                for (int b = 0; b < 26; ++b) {
                    if (a == b) continue;
                    for (int c = 0; c < 26; ++c) {
                        if (a == c || b == c) continue;
                        dp[i+1][b][c] += dp[i][a][b];
                        dp[i+1][b][c] %= MOD;
                    }
                }
            }
        } else {
            int d = s[i] - 'a';
            for (int a = 0; a < 26; ++a) {
                if (d == a) continue;
                for (int b = 0; b < 26; ++b) {
                    if (a == b || d == b) continue;
                    dp[i+1][b][d] += dp[i][a][b];
                    dp[i+1][b][d] %= MOD;
                }
            }
        }
    }

    ll ans = 0;
    for (int i = 0; i < 26; ++i) {
        for (int j = 0; j < 26; ++j) {
            ans += dp[n][i][j];
            ans %= MOD;
        }
    }

    printf("%lld\n", ans);
}
0