Codeforces Round #635 (Div. 1)C(区间DP)

枚举元素s[i],枚举区间[j,r],r=j+i-1,如果s[i]==t[j],则可以把s[i]加到当前区间t串的左侧,如果s[i]==t[r],则可以把s[i]加到当前区间t串的右侧。答案为Sum:dp[1][m]~dp[1][n],m~n均为有效前缀,后面随便放。

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 #define mod 998244353
 4 using namespace std;
 5 char s[3007],t[3007];
 6 int dp[3007][3007];
 7 int main(){
 8     ios::sync_with_stdio(false);
 9     cin.tie(NULL);
10     cout.tie(NULL);
11     cin>>s+1>>t+1;
12     int n=strlen(s+1),m=strlen(t+1);
13     for(int i=1;i<=n;++i)
14         for(int j=1;j<=n+1-i;++j){
15             int r=j+i-1;
16             if(s[i]==t[j]||j>m){
17                 dp[j][r]+=(j==r)?1:dp[j+1][r];
18                 dp[j][r]%=mod;
19             }
20             if(s[i]==t[r]||r>m){
21                 dp[j][r]+=(j==r)?1:dp[j][r-1];
22                 dp[j][r]%=mod;
23             }
24         }
25     int ans=0;
26     for(int i=m;i<=n;++i){
27         ans+=dp[1][i];
28         ans%=mod;
29     }
30     cout<<ans;
31 }

猜你喜欢

转载自www.cnblogs.com/ldudxy/p/12728705.html