emmm,仍然是辣鸡kmp算法,做的我头都秃了
#include <cstdio> #include <cstring> using namespace std; char s[1000005]; int P[1000005],ch[1000005],n,j,len; long long ans; const long long Mod=1e9+7; int main(){ int i; scanf("%d",&n); while(n--){ scanf("%s",s+1); memset(P,0,sizeof(P)); len=strlen(s+1); ans=1; j=0; P[1]=0; ch[1]=1; for(i=1;i<len;i++){ while(j&&s[i+1]!=s[j+1]){ j=P[j]; } j+=(s[i+1]==s[j+1]); P[i+1]=j; ch[i+1]=ch[j]+1; } for(i=1;i<len;i++){ while(j&&s[i+1]!=s[j+1]){ j=P[j]; } j+=(s[i+1]==s[j+1]); while(j>((i+1)>>1)){ j=P[j]; } ans=ans*((ch[j]+1)%Mod)%Mod; } printf("%lld\n",ans%Mod); } return 0; }
原文:https://www.cnblogs.com/ainiyuling/p/11137044.html