CF1336C Kaavi and Magic Spell
题目

分析
好题。
直接做并不好做,然后数据范围提示区间dp。(其实好多这种匹配的都是区间dp,比如 CSP2021-S T2)
于是我们设状态 \(dp[i][j]\) 表示 \(S\) 前 \(j-i+1\) 个字符,当前获得了 \(T_{i...j}\) 的方案数。(对于 \(i>siz_t\) , \(T_i\) 可以是任意字符)
然后状态转移方程很好写,判断是否字符相等转移即可。
最后答案就是 \(\sum_{i=m}^n dp[1][i]\) 。
代码
#include<bits/stdc++.h>
using namespace std;
//#ifdef ONLINE_JUDGE
// #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
// char buf[1<<21],*p1=buf,*p2=buf;
//#endif
template<typename T>
inline void read(T &x){
x=0;bool f=false;char ch=getchar();
while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
while(isdigit(ch)){x=x*10+(ch^48);ch=getchar();}
x=f?-x:x;
return ;
}
template<typename T>
inline void write(T x){
if(x<0) x=-x,putchar('-');
if(x>9) write(x/10);
putchar(x%10^48);
return ;
}
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pc putchar
#define PII pair<int,int>
#define rep(i,x,y) for(register int i=(x);i<=(y);i++)
#define dep(i,y,x) for(register int i=(y);i>=(x);i--)
const int MOD=998244353;
inline int inc(int x,int y){x+=y;return x>=MOD?x-MOD:x;}
inline int dec(int x,int y){x-=y;return x<0?x+MOD:x;}
inline void incc(int &x,int y){x+=y;if(x>=MOD) x-=MOD;}
inline void decc(int &x,int y){x-=y;if(x<0) x+=MOD;}
inline void chkmin(int &x,int y){if(y<x) x=y;}
inline void chkmax(int &x,int y){if(y>x) x=y;}
const int N=5005,M=2e5+5,INF=1e9+7,V=3e5;
int n,m,dp[N][N],Ans;
char str1[N],str2[N];
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(false);
// double ST=clock();
scanf("%s",str1+1),scanf("%s",str2+1);
n=strlen(str1+1),m=strlen(str2+1);
for(int i=1;i<=n;i++) dp[i][i]=((str1[1]==str2[i]||i>m)?2:0);
for(int len=2;len<=n;len++){
for(int l=1;l+len-1<=n;l++){
int r=l+len-1;
if(l>m||str2[l]==str1[len]) incc(dp[l][r],dp[l+1][r]);
if(r>m||str2[r]==str1[len]) incc(dp[l][r],dp[l][r-1]);
}
}
for(int i=m;i<=n;i++) incc(Ans,dp[1][i]);
write(Ans);
//#ifndef ONLINE_JUDGE
// cerr<<"\nTime:"<<(clock()-ST)/CLOCKS_PER_SEC<<"s\n";
//#endif
return 0;
}
/*
7 6
1 2
1 3
1 4
3 5
3 6
4 7
*/

浙公网安备 33010602011771号