結果

問題 No.1646 Avoid Palindrome
ユーザー SSRSSSRS
提出日時 2021-08-13 21:28:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,577 ms / 3,000 ms
コード長 1,259 bytes
コンパイル時間 1,842 ms
コンパイル使用メモリ 171,064 KB
実行使用メモリ 320,384 KB
最終ジャッジ日時 2024-04-26 02:30:50
合計ジャッジ時間 71,772 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 1,944 ms
301,952 KB
testcase_05 AC 1,957 ms
301,952 KB
testcase_06 AC 1,877 ms
291,712 KB
testcase_07 AC 1,942 ms
301,952 KB
testcase_08 AC 1,963 ms
304,640 KB
testcase_09 AC 1,862 ms
289,792 KB
testcase_10 AC 1,903 ms
296,960 KB
testcase_11 AC 1,861 ms
288,640 KB
testcase_12 AC 1,942 ms
301,568 KB
testcase_13 AC 1,965 ms
304,768 KB
testcase_14 AC 2,380 ms
295,936 KB
testcase_15 AC 2,418 ms
302,080 KB
testcase_16 AC 2,352 ms
294,272 KB
testcase_17 AC 2,477 ms
307,072 KB
testcase_18 AC 2,399 ms
298,880 KB
testcase_19 AC 2,374 ms
297,472 KB
testcase_20 AC 2,407 ms
300,032 KB
testcase_21 AC 2,465 ms
306,688 KB
testcase_22 AC 2,375 ms
296,448 KB
testcase_23 AC 2,470 ms
308,352 KB
testcase_24 AC 2,566 ms
320,256 KB
testcase_25 AC 2,562 ms
320,128 KB
testcase_26 AC 2,566 ms
320,256 KB
testcase_27 AC 2,562 ms
320,256 KB
testcase_28 AC 2,577 ms
320,128 KB
testcase_29 AC 1,698 ms
320,256 KB
testcase_30 AC 1,682 ms
320,128 KB
testcase_31 AC 1,652 ms
320,128 KB
testcase_32 AC 1,673 ms
320,256 KB
testcase_33 AC 1,664 ms
320,256 KB
testcase_34 AC 2,570 ms
320,128 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 1,659 ms
320,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
int main(){
  int N;
  cin >> N;
  string S;
  cin >> S;
  if (N == 1){
    if (S[0] == '?'){
      cout << 26 << endl;
    } else {
      cout << 1 << endl;
    }
  } else {
    vector<vector<vector<long long>>> dp(N + 1, vector<vector<long long>>(26, vector<long long>(26, 0)));
    for (int i = 0; i < 26; i++){
      if (S[0] == '?' || S[0] - 'a' == i){
        for (int j = 0; j < 26; j++){
          if (S[1] == '?' || S[1] - 'a' == j){
            if (i != j){
              dp[2][i][j]++;
            }
          }
        }
      }
    }
    for (int i = 2; i < N; i++){
      for (int j = 0; j < 26; j++){
        for (int k = 0; k < 26; k++){
          for (int l = 0; l < 26; l++){
            if (S[i] == '?' || S[i] - 'a' == l){
              if (j != l && k != l){
                dp[i + 1][k][l] += dp[i][j][k];
                if (dp[i + 1][k][l] >= MOD){
                  dp[i + 1][k][l] -= MOD;
                }
              }
            }
          }
        }
      }
    }
    long long ans = 0;
    for (int i = 0; i < 26; i++){
      for (int j = 0; j < 26; j++){
        ans += dp[N][i][j];
      }
    }
    ans %= MOD;
    cout << ans << endl;
  }
}
0