結果

問題 No.1646 Avoid Palindrome
ユーザー startcppstartcpp
提出日時 2021-08-13 22:27:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,515 ms / 3,000 ms
コード長 971 bytes
コンパイル時間 546 ms
コンパイル使用メモリ 66,468 KB
実行使用メモリ 146,176 KB
最終ジャッジ日時 2024-04-26 03:15:45
合計ジャッジ時間 29,184 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 325 ms
136,832 KB
testcase_05 AC 322 ms
136,832 KB
testcase_06 AC 313 ms
132,224 KB
testcase_07 AC 321 ms
136,832 KB
testcase_08 AC 325 ms
137,984 KB
testcase_09 AC 309 ms
131,456 KB
testcase_10 AC 314 ms
134,784 KB
testcase_11 AC 300 ms
131,072 KB
testcase_12 AC 322 ms
136,576 KB
testcase_13 AC 319 ms
138,240 KB
testcase_14 AC 1,390 ms
135,296 KB
testcase_15 AC 1,430 ms
137,856 KB
testcase_16 AC 1,403 ms
134,400 KB
testcase_17 AC 1,454 ms
140,032 KB
testcase_18 AC 1,427 ms
136,576 KB
testcase_19 AC 1,393 ms
135,936 KB
testcase_20 AC 1,421 ms
136,960 KB
testcase_21 AC 1,445 ms
140,032 KB
testcase_22 AC 1,395 ms
135,296 KB
testcase_23 AC 1,462 ms
140,672 KB
testcase_24 AC 1,511 ms
145,920 KB
testcase_25 AC 1,504 ms
145,920 KB
testcase_26 AC 1,515 ms
146,048 KB
testcase_27 AC 1,500 ms
145,920 KB
testcase_28 AC 1,506 ms
146,176 KB
testcase_29 AC 173 ms
142,848 KB
testcase_30 AC 177 ms
142,848 KB
testcase_31 AC 178 ms
142,720 KB
testcase_32 AC 176 ms
142,592 KB
testcase_33 AC 174 ms
143,104 KB
testcase_34 AC 1,503 ms
146,048 KB
testcase_35 AC 2 ms
6,944 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 1 ms
6,940 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 2 ms
6,944 KB
testcase_42 AC 1 ms
6,944 KB
testcase_43 AC 177 ms
142,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//まずはTLEしそうな解法から
#include <iostream>
#include <string>
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;

const int mod = 998244353;
int n;
string s;
int dp[50001][27][27];	//dp[len][2文字前][直前], ブランク: 26番

signed main() {
	int i, j, k, l;
	
	cin >> n >> s;
	
	dp[0][26][26] = 1;
	rep(i, n) {
		rep(j, 27) {
			rep(k, 27) {
				if (dp[i][j][k] == 0) continue;
				if (s[i] != '?') {
					l = s[i] - 'a';
					if (j == l || k == l) continue;
					dp[i + 1][k][l] += dp[i][j][k];
					if (dp[i + 1][k][l] >= mod) dp[i + 1][k][l] -= mod;
				}
				else {
					rep(l, 26) { //ブランク = 26は含まないので注意
						if (j == l || k == l) continue;
						dp[i + 1][k][l] += dp[i][j][k];
						if (dp[i + 1][k][l] >= mod) dp[i + 1][k][l] -= mod;
					}
				}
			}
		}
	}
	
	int ans = 0;
	rep(j, 27) {
		rep(k, 27) {
			ans += dp[n][j][k];
			if (ans >= mod) ans -= mod;
		}
	}
	
	cout << ans << endl;
	return 0;
}
0