結果

問題 No.1646 Avoid Palindrome
ユーザー 夜
提出日時 2021-08-14 09:42:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 765 ms / 3,000 ms
コード長 1,607 bytes
コンパイル時間 978 ms
コンパイル使用メモリ 95,160 KB
実行使用メモリ 355,456 KB
最終ジャッジ日時 2024-04-26 03:43:36
合計ジャッジ時間 24,272 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 2 ms
6,940 KB
testcase_04 AC 625 ms
335,080 KB
testcase_05 AC 620 ms
334,972 KB
testcase_06 AC 616 ms
323,640 KB
testcase_07 AC 621 ms
334,976 KB
testcase_08 AC 633 ms
338,192 KB
testcase_09 AC 604 ms
321,672 KB
testcase_10 AC 612 ms
329,492 KB
testcase_11 AC 576 ms
320,348 KB
testcase_12 AC 634 ms
334,332 KB
testcase_13 AC 620 ms
338,152 KB
testcase_14 AC 704 ms
328,576 KB
testcase_15 AC 722 ms
335,104 KB
testcase_16 AC 704 ms
326,528 KB
testcase_17 AC 726 ms
340,608 KB
testcase_18 AC 711 ms
331,904 KB
testcase_19 AC 708 ms
330,112 KB
testcase_20 AC 711 ms
332,672 KB
testcase_21 AC 723 ms
340,352 KB
testcase_22 AC 707 ms
328,704 KB
testcase_23 AC 731 ms
342,144 KB
testcase_24 AC 763 ms
355,200 KB
testcase_25 AC 765 ms
355,456 KB
testcase_26 AC 759 ms
355,328 KB
testcase_27 AC 760 ms
355,200 KB
testcase_28 AC 765 ms
355,328 KB
testcase_29 AC 673 ms
355,368 KB
testcase_30 AC 628 ms
355,240 KB
testcase_31 AC 639 ms
355,308 KB
testcase_32 AC 662 ms
355,240 KB
testcase_33 AC 625 ms
355,416 KB
testcase_34 AC 759 ms
355,328 KB
testcase_35 AC 2 ms
6,944 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,944 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 634 ms
355,108 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