結果

問題 No.1646 Avoid Palindrome
ユーザー 👑 platinumplatinum
提出日時 2021-08-13 23:08:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 371 ms / 3,000 ms
コード長 2,036 bytes
コンパイル時間 2,286 ms
コンパイル使用メモリ 198,380 KB
実行使用メモリ 367,176 KB
最終ジャッジ日時 2024-04-26 03:26:55
合計ジャッジ時間 17,689 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 212 ms
366,792 KB
testcase_01 AC 211 ms
367,044 KB
testcase_02 AC 215 ms
366,912 KB
testcase_03 AC 214 ms
367,048 KB
testcase_04 AC 359 ms
366,916 KB
testcase_05 AC 358 ms
366,916 KB
testcase_06 AC 354 ms
366,916 KB
testcase_07 AC 360 ms
366,916 KB
testcase_08 AC 362 ms
366,920 KB
testcase_09 AC 353 ms
367,044 KB
testcase_10 AC 349 ms
367,168 KB
testcase_11 AC 350 ms
367,044 KB
testcase_12 AC 351 ms
367,172 KB
testcase_13 AC 356 ms
367,172 KB
testcase_14 AC 340 ms
367,044 KB
testcase_15 AC 342 ms
366,916 KB
testcase_16 AC 339 ms
367,044 KB
testcase_17 AC 343 ms
367,048 KB
testcase_18 AC 343 ms
366,920 KB
testcase_19 AC 343 ms
367,040 KB
testcase_20 AC 342 ms
366,912 KB
testcase_21 AC 350 ms
366,916 KB
testcase_22 AC 347 ms
367,044 KB
testcase_23 AC 345 ms
367,048 KB
testcase_24 AC 354 ms
366,916 KB
testcase_25 AC 351 ms
367,168 KB
testcase_26 AC 350 ms
367,044 KB
testcase_27 AC 353 ms
367,168 KB
testcase_28 AC 350 ms
367,176 KB
testcase_29 AC 366 ms
366,920 KB
testcase_30 AC 370 ms
367,048 KB
testcase_31 AC 369 ms
367,048 KB
testcase_32 AC 371 ms
367,040 KB
testcase_33 AC 366 ms
367,044 KB
testcase_34 AC 352 ms
367,048 KB
testcase_35 AC 212 ms
366,920 KB
testcase_36 AC 211 ms
366,916 KB
testcase_37 AC 215 ms
366,916 KB
testcase_38 AC 221 ms
366,784 KB
testcase_39 AC 212 ms
366,792 KB
testcase_40 AC 216 ms
367,044 KB
testcase_41 AC 211 ms
366,916 KB
testcase_42 AC 212 ms
366,788 KB
testcase_43 AC 370 ms
367,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int,int>;
using vv = vector<vector<int>>;
const int INF = (int)1e9;
const LL LINF = (LL)1e18;

long long const mod = 998244353;
struct mint{
	long long val;
	mint(long long val = 0): val(val % mod) {}
	mint& operator += (const mint n){
		val += n.val;
		if(val >= mod) val -= mod;
		return *this;
	}
	mint& operator -= (const mint n){
		val -= n.val;
		if(val < 0) val += mod;
		return *this;
	}
	mint& operator *= (const mint n){
		val *= n.val;
		val %= mod;
		if(val < 0) val += mod;
		return *this;
	}
	mint operator + (const mint n) const{
		mint res(*this);
		return res += n;
	}
	mint operator - (const mint n) const{
		mint res(*this);
		return res -= n;
	}
	mint operator * (const mint n) const{
		mint res(*this);
		return res *= n;
	}
	mint pow(long long n) const{
		if(n == 0) return 1;
		mint m = pow(n >> 1);
		m *= m;
		if(n & 1) m *= *this;
		return m;
	}
	// mint division for prime mod
	mint inv() const{
		return pow(mod - 2);
	}
	mint& operator /= (const mint n){
		return (*this) *= n.inv();
	}
	mint operator / (const mint n) const{
		mint res(*this);
		return res /= n;
	}
};

mint dp[50010][30][30];
mint sum[50010][30];

int main(){
	int N;
	string S;
	cin >> N >> S;
	if(N == 1){
		if(S[0] == '?') cout << 26 << endl;
		else cout << 1 << endl;
		return 0;
	}
	dp[0][0][0] = 1;
	sum[0][0] = 1;
	rep(i,N){
		int c = S[i] - 'a' + 1;
		rep(j,27){
			if(i > 1 and j == 0) continue;
			rep(k,27){
				if(i > 0 and k == 0) continue;
				if(j == k and j > 0) continue;
				if(S[i] == '?'){
					if(j == 0 and k == 0) continue;
					dp[i+1][j][k] += sum[i][j];
					dp[i+1][j][k] -= dp[i][k][j];
					sum[i+1][k] += dp[i+1][j][k];
				}
				else{
					if(c == j or c == k) continue;
					dp[i+1][k][c] += dp[i][j][k];
				}
			}
		}
		rep(j,27) sum[i+1][c] += dp[i+1][j][c];
	}
	mint ans = 0;
	rep(j,26){
		rep(k,26) ans += dp[N][j+1][k+1];
	}
	cout << ans.val << endl;

	return 0;
}
0