結果
| 問題 |
No.1646 Avoid Palindrome
|
| コンテスト | |
| ユーザー |
platinum
|
| 提出日時 | 2021-08-13 23:08:37 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 448 ms / 3,000 ms |
| コード長 | 2,036 bytes |
| コンパイル時間 | 2,261 ms |
| コンパイル使用メモリ | 194,212 KB |
| 最終ジャッジ日時 | 2025-01-23 21:05:56 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 40 |
ソースコード
#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;
}
platinum