結果

問題 No.1646 Avoid Palindrome
ユーザー 夜
提出日時 2021-08-14 09:42:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 741 ms / 3,000 ms
コード長 1,607 bytes
コンパイル時間 853 ms
コンパイル使用メモリ 92,316 KB
実行使用メモリ 355,164 KB
最終ジャッジ日時 2023-08-08 10:32:31
合計ジャッジ時間 24,033 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 608 ms
334,780 KB
testcase_05 AC 607 ms
334,780 KB
testcase_06 AC 590 ms
323,360 KB
testcase_07 AC 612 ms
334,948 KB
testcase_08 AC 604 ms
338,084 KB
testcase_09 AC 578 ms
321,504 KB
testcase_10 AC 589 ms
329,416 KB
testcase_11 AC 573 ms
320,156 KB
testcase_12 AC 609 ms
334,208 KB
testcase_13 AC 625 ms
338,032 KB
testcase_14 AC 685 ms
328,252 KB
testcase_15 AC 695 ms
334,856 KB
testcase_16 AC 680 ms
326,296 KB
testcase_17 AC 707 ms
340,604 KB
testcase_18 AC 690 ms
331,480 KB
testcase_19 AC 687 ms
329,820 KB
testcase_20 AC 693 ms
332,524 KB
testcase_21 AC 707 ms
340,192 KB
testcase_22 AC 687 ms
328,564 KB
testcase_23 AC 713 ms
342,032 KB
testcase_24 AC 740 ms
355,160 KB
testcase_25 AC 738 ms
354,980 KB
testcase_26 AC 740 ms
355,064 KB
testcase_27 AC 741 ms
355,104 KB
testcase_28 AC 739 ms
355,100 KB
testcase_29 AC 656 ms
354,976 KB
testcase_30 AC 576 ms
354,976 KB
testcase_31 AC 565 ms
354,992 KB
testcase_32 AC 574 ms
355,088 KB
testcase_33 AC 571 ms
354,972 KB
testcase_34 AC 741 ms
355,032 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 564 ms
355,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
long long mod = 998244353;
long long dp[50050][30][30] = {};
long long sum1[30] = {};
int main() {
	long long n, co = 0;
	string s;
	cin >> n >> s;
	if (n == 1) {
		if (s == "?") {
			cout << 26 << endl;
		}
		else {
			cout << 1 << endl;
		}
		return 0;
	}
	for (int i = 0; i < 26; i++) {
		for (int j = 0; j < 26; j++) {
			int i1 = i, j1 = j;
			if (s[0] != '?') {
				i1 = s[0] - 'a';
			}
			if (s[1] != '?') {
				j1 = s[1] - 'a';
			}
			if (i1 != j1) {
				dp[2][j1][i1] = 1;
			}
		}
	}
	for (int j = 0; j < 26; j++) {
		for (int k = 0; k < 26; k++) {
			sum1[j] += dp[2][j][k];
		}
	}
	for (int i = 2; i < n; i++) {
		if (s[i] == '?') {
			for (int j = 0; j < 26; j++) {
				for (int k = 0; k < 26; k++) {
					if (j != k) {
						dp[i + 1][j][k] += sum1[k];
						dp[i + 1][j][k] -= dp[i][k][j];
					}
				}
			}
		}
		else {
			for (int j = 0; j < 26; j++) {
				if (s[i] - 'a' != j) {
					dp[i + 1][s[i] - 'a'][j] += sum1[j];
					dp[i + 1][s[i] - 'a'][j] -= dp[i][j][s[i] - 'a'];
				}
			}
		}
		for (int j = 0; j < 26; j++) {
			sum1[j] = 0;
			for (int k = 0; k < 26; k++) {
				dp[i + 1][j][k] %= mod;
				sum1[j] += dp[i + 1][j][k];
			}
		}
	}
	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