其他分享
首页 > 其他分享> > 暑假集训Day3 J(lcm系列问题)

暑假集训Day3 J(lcm系列问题)

作者:互联网

 

 

 

 

本质上还是lcm问题,我们设f[i][j]为到s串的第i位(第i位必选),t串的第j位,符合条件的个数,

***注意这里第i位是必选的***,

这样状态统计的时候就不会混,最后ans就f[i][m]求个和就行,注意到由于t串是多个字符的,所以在求f[i][1]和f[i][j]的时候还是有点差别的。

 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 typedef long long LL;
 4 const int MAX=1e5+5;
 5 LL n,m,now1,now2,ans;
 6 char s[MAX],t[105];
 7 LL f[MAX][105],g[MAX][105];
 8 int main(){
 9 //    freopen ("j.in","r",stdin);
10 //    freopen ("j.out","w",stdout);
11     int i,j;
12     scanf("%s%s",s+1,t+1);
13     n=strlen(s+1);
14     m=strlen(t+1);
15     memset(f,0,sizeof(f));
16     memset(g,0,sizeof(g));
17     f[0][0]=0;
18     for (i=1;i<=n;i++) f[i][0]=f[i-1][0]+1;
19     for (i=1;i<=n;i++){
20         for (j=1;j<=m;j++){
21             if (s[i]==t[j]){
22                 if (j==1)
23                     f[i][j]+=f[i-1][j-1]+1;
24                 else f[i][j]+=f[i-1][j-1];
25             }
26             else f[i][j]+=f[i-1][j];
27         }
28         ans+=f[i][m];
29     }
30     printf("%lld",ans);
31     return 0;
32 }

 

标签:int,MAX,LL,memset,Day3,ans,lcm,集训,105
来源: https://www.cnblogs.com/keximeiruguo/p/16463959.html