Simple DP+KMP. Dp[i] is a kind of meaning that can be expressed in 0--i.
If the suffix ending with s[i] does not match, then dp[i]=dp[i-1].
If S[i] is the end of the suffix match, then dp[i]=dp[i-1]+dp[i-len]; (That is, the number of species expressing the first meaning + the number of species that express the second meaning)
#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<Set>#include<queue>#include<stack>#include<iostream>using namespaceStd;typedefLong LongLL;Const DoublePi=acos (-1.0), eps=1e-8;voidFile () {freopen ("D:\\in.txt","R", stdin); Freopen ("D:\\out.txt","W", stdout);} InlineintRead () {Charc = GetChar (); while(!isdigit (c)) C =GetChar (); intx =0; while(IsDigit (c)) {x = x *Ten+ C-'0'; c =GetChar ();} returnx;} LL MoD=1000000007;Const intmaxn=100010;intNX[MAXN],F[MAXN];CharA[MAXN],B[MAXN];intLen1,len2; LL ANS[MAXN];voidGet_next () {intj=-1, i=0; nx[0]=-1; while(i<len2) { if(j==-1|| B[I]==B[J]) i++, j + +, nx[i]=J; Elsej=Nx[j]; }}voidKMP () {get_next (); intI=0, j=0; while(i<len1) { if(j==-1|| A[I]==B[J]) i++, j + +; Elsej=Nx[j]; if(J==LEN2) f[i-j+len2]=1; }}intMain () {intT scanf"%d", &t);intcas=1; while(t--) {scanf ("%s%s", A, b); Len1=strlen (a), len2=strlen (b); Memset (F,0,sizeoff); KMP (); ans[0]=1; for(intI=1; i<=len1;i++) {Ans[i]=ans[i-1]; if(f[i]==1) ans[i]= (Ans[i]+ans[i-len2])%MoD; } printf ("Case #%d:%lld\n", cas++, Ans[len1]); } return 0;}
HDU 5763 Another meaning