結果

問題 No.1646 Avoid Palindrome
ユーザー startcppstartcpp
提出日時 2021-08-13 22:27:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,224 ms / 3,000 ms
コード長 971 bytes
コンパイル時間 868 ms
コンパイル使用メモリ 64,944 KB
実行使用メモリ 146,088 KB
最終ジャッジ日時 2023-08-08 10:02:30
合計ジャッジ時間 41,870 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 405 ms
136,688 KB
testcase_05 AC 403 ms
136,628 KB
testcase_06 AC 399 ms
131,948 KB
testcase_07 AC 396 ms
136,640 KB
testcase_08 AC 401 ms
137,696 KB
testcase_09 AC 379 ms
131,308 KB
testcase_10 AC 385 ms
134,612 KB
testcase_11 AC 377 ms
130,776 KB
testcase_12 AC 403 ms
136,448 KB
testcase_13 AC 403 ms
138,040 KB
testcase_14 AC 2,051 ms
135,004 KB
testcase_15 AC 2,092 ms
137,620 KB
testcase_16 AC 2,040 ms
134,248 KB
testcase_17 AC 2,125 ms
140,176 KB
testcase_18 AC 2,064 ms
136,512 KB
testcase_19 AC 2,057 ms
135,724 KB
testcase_20 AC 2,079 ms
136,836 KB
testcase_21 AC 2,121 ms
139,760 KB
testcase_22 AC 2,049 ms
135,072 KB
testcase_23 AC 2,135 ms
140,556 KB
testcase_24 AC 2,216 ms
145,896 KB
testcase_25 AC 2,223 ms
145,944 KB
testcase_26 AC 2,220 ms
146,000 KB
testcase_27 AC 2,220 ms
145,820 KB
testcase_28 AC 2,214 ms
146,088 KB
testcase_29 AC 181 ms
142,676 KB
testcase_30 AC 180 ms
142,588 KB
testcase_31 AC 180 ms
142,504 KB
testcase_32 AC 181 ms
142,524 KB
testcase_33 AC 180 ms
142,648 KB
testcase_34 AC 2,224 ms
145,820 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,384 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 179 ms
142,720 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