标签:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; const long long mod=1e9+7; char s[100005]; char ss[100005]; int next1[100005]; long long num[100005]; int pos[100005]; void makenext1(const char P[]) { int q,k; int m = strlen(P); next1[0]=0; for (q = 1,k = 0; q < m; ++q) { while(k > 0 && P[q] != P[k]) k = next1[k-1]; if (P[q] == P[k]) { k++; } next1[q] = k; } } long long calc(char T[],char P[]) { int n,m; int i,q; int tot=0; n = strlen(T); m = strlen(P); makenext1(P); for(i=0,q = 0; i < n; ++i) { while(q>0&&P[q]!=T[i]) q=next1[q-1]; if(P[q]==T[i]) { q++; } if(q==m) { long long flag=1; pos[tot]=i-m+1; if(tot>0) { if(pos[tot-1]+m<=pos[tot]) { num[tot]=(2*num[tot-1])%mod; } else { num[tot]=num[tot-1]%mod; for(int h=tot-2;h>=0;h--) { if(pos[h]+m<=pos[tot]) { num[tot]=(num[tot]+num[h])%mod; flag=0; ///当之前不存在不相重叠的语句时; break; } } num[tot]=(num[tot]+flag)%mod; } } else { num[tot]=2; } tot++; } } if(tot==0) return 1; return num[tot-1]; } int main() { int T; int Case=1; cin>>T; while(T--) { scanf("%s%s",s,ss); printf("Case #%d: %lld\n",Case++,calc(s,ss)); // cout<<(calc(s,ss)%mod+mod)%mod<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/chen9510/p/5728752.html