結果

問題 No.1646 Avoid Palindrome
ユーザー lolklolk
提出日時 2021-08-18 20:54:44
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 2,258 ms / 3,000 ms
コード長 1,567 bytes
コンパイル時間 425 ms
コンパイル使用メモリ 102,528 KB
実行使用メモリ 267,136 KB
最終ジャッジ日時 2024-04-26 03:52:22
合計ジャッジ時間 52,249 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1,237 ms
251,904 KB
testcase_05 AC 1,226 ms
251,776 KB
testcase_06 AC 1,161 ms
243,328 KB
testcase_07 AC 1,217 ms
251,904 KB
testcase_08 AC 1,220 ms
254,208 KB
testcase_09 AC 1,164 ms
241,792 KB
testcase_10 AC 1,187 ms
247,680 KB
testcase_11 AC 1,161 ms
240,896 KB
testcase_12 AC 1,228 ms
251,392 KB
testcase_13 AC 1,230 ms
254,208 KB
testcase_14 AC 2,068 ms
247,040 KB
testcase_15 AC 2,125 ms
251,904 KB
testcase_16 AC 2,076 ms
245,504 KB
testcase_17 AC 2,171 ms
256,256 KB
testcase_18 AC 2,115 ms
249,344 KB
testcase_19 AC 2,096 ms
248,064 KB
testcase_20 AC 2,111 ms
250,112 KB
testcase_21 AC 2,146 ms
256,000 KB
testcase_22 AC 2,096 ms
247,168 KB
testcase_23 AC 2,176 ms
257,280 KB
testcase_24 AC 2,250 ms
267,136 KB
testcase_25 AC 2,249 ms
267,008 KB
testcase_26 AC 2,245 ms
267,008 KB
testcase_27 AC 2,258 ms
267,136 KB
testcase_28 AC 2,253 ms
267,136 KB
testcase_29 AC 641 ms
267,008 KB
testcase_30 AC 641 ms
267,008 KB
testcase_31 AC 639 ms
267,008 KB
testcase_32 AC 644 ms
267,008 KB
testcase_33 AC 639 ms
267,008 KB
testcase_34 AC 2,258 ms
267,136 KB
testcase_35 AC 1 ms
6,940 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 1 ms
6,940 KB
testcase_38 AC 1 ms
6,940 KB
testcase_39 AC 1 ms
6,940 KB
testcase_40 AC 1 ms
6,940 KB
testcase_41 AC 1 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 641 ms
267,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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