結果

問題 No.1646 Avoid Palindrome
ユーザー simansiman
提出日時 2023-08-01 17:12:24
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,625 bytes
コンパイル時間 1,452 ms
コンパイル使用メモリ 138,616 KB
実行使用メモリ 267,648 KB
最終ジャッジ日時 2024-04-19 16:26:37
合計ジャッジ時間 15,374 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 201 ms
267,648 KB
testcase_03 WA -
testcase_04 AC 335 ms
267,520 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 322 ms
267,520 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 188 ms
267,520 KB
testcase_38 AC 198 ms
267,392 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 192 ms
267,392 KB
testcase_42 WA -
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const ll MOD = 998244353;
ll dp[50010][26][26];

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;

  if (N == 1) {
    if (S[0] == '?') {
      cout << 26 << endl;
    } else {
      cout << 1 << endl;
    }
    return 0;
  }

  memset(dp, 0, sizeof(dp));

  for (int u = 0; u < 26; ++u) {
    if (S[0] != '?' && S[0] - 'a' != u) continue;

    for (int v = 0; v < 26; ++v) {
      if (S[1] != '?' && S[1] - 'a' != u) continue;
      dp[1][u][v]++;
    }
  }

  for (int i = 2; i < N; ++i) {
    char s = S[i];
    ll sum[26];
    memset(sum, 0, sizeof(sum));

    for (int u = 0; u < 26; ++u) {
      for (int v = 0; v < 26; ++v) {
        sum[v] += dp[i - 1][u][v];
        sum[v] %= MOD;
      }
    }

    if (s == '?') {
      for (int u = 0; u < 26; ++u) {
        for (int v = 0; v < 26; ++v) {
          if (u == v) continue;

          dp[i][u][v] += (sum[u] - dp[i - 1][v][u] + MOD) % MOD;
          dp[i][u][v] %= MOD;
        }
      }
    } else {
      int v = s - 'a';
      for (int u = 0; u < 26; ++u) {
        if (u == v) continue;
        dp[i][u][v] += (sum[u] - dp[i - 1][v][u] + MOD) % MOD;
        dp[i][u][v] %= MOD;
      }
    }
  }

  ll ans = 0;

  for (int u = 0; u < 26; ++u) {
    for (int v = 0; v < 26; ++v) {
      if (u == v) continue;

      ans += dp[N - 1][u][v];
      ans %= MOD;
    }
  }

  cout << ans << endl;

  return 0;
}
0