題目
給定一個長度爲n(n<=3e3)的串S和一個長度爲m(m<=n)的串T
每次可以維護一個雙端隊列A,可以把當前S串首的位置刪掉,補充到A的隊首或隊尾
n次操作後,A會構成一個串,求串T是A的前綴的方案數,答案mod998244353
思路來源
凡神代碼
題解
orz,又一次卡在了思維題上,
把T後面補齊?號,表示通配符,
考慮S的第一個字母總要與T的某一個位置對應,有隊首或隊尾兩種方式,
之後只能在第一次的位置的兩側進行擴展,可從小區間轉移而來
主要是通配符的這個idea沒想到吧orz
代碼
#include<bits/stdc++.h>
using namespace std;
const int N=3e3+10,mod=998244353;
int n,m,dp[N][N];
char s[N],t[N];
bool ok(char x,char y){
return y=='?'||(x==y);
}
void add(int &x,int y){
x+=y;
if(x>=mod)x-=mod;
}
int main(){
scanf("%s%s",s,t);
n=strlen(s),m=strlen(t);
for(int i=m;i<n;++i)t[i]='?';//後面的位置通配
for(int i=0;i<n;++i){
dp[i][i]=ok(s[0],t[i])?2:0;
}
for(int len=2;len<=n;++len){
for(int l=0;l+len-1<n;++l){
int r=l+len-1;
if(ok(s[len-1],t[l]))add(dp[l][r],dp[l+1][r]);
if(ok(s[len-1],t[r]))add(dp[l][r],dp[l][r-1]);
}
}
int ans=0;
for(int i=m-1;i<n;++i){
add(ans,dp[0][i]);
}
printf("%d\n",ans);
return 0;
}