标签:枚举 pre otto code accept inpu title ini 伪代码
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 612 Accepted Submission(s): 237
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=1e6+50; const int mod=1e9+7; char chang[N],duan[N]; int nex[N],f[N]; int len1,len2; void get() { int i=0,j=-1; nex[0]=-1; while(i<len2) { if(j==-1||duan[i]==duan[j]) nex[++i]=++j; else j=nex[j]; } f[0]=0; for(int i=1; i<=len2; ++i) f[i]=(f[nex[i]]+i)%mod; } int KMP() { get(); int i=0,j=0,ans=0; while(i<len1) { while(~j&&chang[i]!=duan[j]) { j=nex[j]; } ++i;++j; ans=(ans+f[j])%mod; if(j==len2) j=nex[j]; } return ans%mod; } int main() { int T; for(scanf("%d",&T); T--;) { scanf("%s %s",chang,duan); len1=strlen(chang),len2=strlen(duan); for(int i=0; i<len1/2; ++i) swap(chang[i],chang[len1-1-i]); for(int i=0; i<len2/2; ++i) swap(duan[i],duan[len2-i-1]); printf("%d\n",KMP()); } }
标签:枚举 pre otto code accept inpu title ini 伪代码
原文地址:http://www.cnblogs.com/mfys/p/7398193.html