P3618 误会
链接:Miku
-----------------------
hash?不不不,kmp走起
--------------------------
唯一的问题是最后要有一个dp来解决
-----------------------------
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int n; const int maxn=100001; char a[maxn],b[maxn]; int la,lb; int ans,j; int ne[maxn]; int mod=1000000007; int pl[maxn]; long long dp[maxn]; int main(){ scanf("%d",&n); for(register int i=1;i<=n;++i){ scanf("%s%s",a+1,b+1); ans=0; la=strlen(a+1); lb=strlen(b+1); memset(dp,0,sizeof(dp)); memset(pl,0,sizeof(pl)); memset(ne,0,sizeof(ne)); j=0; ne[0]=ne[1]=0; for(register int k=2;k<=lb;++k){ while(j&&b[k]!=b[j+1]) j=ne[j]; if(b[k]==b[j+1]) j++; ne[k]=j; } j=0; for(register int k=1;k<=la;++k){ while(j&&a[k]!=b[j+1]) j=ne[j]; if(a[k]==b[j+1]) j++; if(j==lb){ pl[k-lb+1]=1; j=ne[j]; } } dp[0]=1; for(register int j=1;j<=la;++j){ if(pl[j-lb+1]&&j>=lb)dp[j]=(dp[j-lb]+dp[j-1])%mod; else{ dp[j]=dp[j-1]%mod; } } printf("Case #%d: %lld\n",i,dp[la]); } }