昨天刚刚学习了EXKMP
于是我就用了EXKMP做了这道题
找完pre后就用前缀和就好
为了节约清空的时间,我加了时间戳t[]
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<string> 7 #include<cmath> 8 #include<ctime> 9 #include<queue> 10 #include<stack> 11 #include<map> 12 #include<set> 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--) 14 #define re(i,l,r) for(int i=(l);i<=(r);i++) 15 #define Clear(a,b) memset(a,b,sizeof(a)) 16 #define inout(x) printf("%d",(x)) 17 #define douin(x) scanf("%lf",&x) 18 #define strin(x) scanf("%s",(x)) 19 #define LLin(x) scanf("%lld",&x) 20 #define op operator 21 #define CSC main 22 typedef unsigned long long ULL; 23 typedef const int cint; 24 typedef long long LL; 25 using namespace std; 26 void inin(int &ret) 27 { 28 ret=0;int f=0;char ch=getchar(); 29 while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} 30 while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar(); 31 ret=f?-ret:ret; 32 } 33 int n,pre[1000010]; 34 char s[1000010]; 35 void getpre() 36 { 37 int len=strlen(s),a=0; 38 pre[0]=len; 39 while(a<len-1&&s[a]==s[a+1])a++; 40 pre[1]=a; 41 a=1; 42 re(k,2,len-1) 43 { 44 int p=a+pre[a]-1,l=pre[k-a]; 45 if(k-1+l>=p) 46 { 47 int j=(p-k+1)>0?p-k+1:0; 48 while(k+j<len&&s[k+j]==s[j])j++; 49 pre[k]=j; 50 a=k; 51 } 52 else pre[k]=l; 53 } 54 } 55 cint mod=1e9+7; 56 int sum[1000010],t[1000010]; 57 int CSC() 58 { 59 inin(n);int T=0; 60 while(n--) 61 { 62 T++; 63 strin(s); 64 int len=strlen(s); 65 getpre(); 66 re(i,1,len-1) 67 { 68 int r=min(i<<1,pre[i]+i); 69 if(t[i]!=T)t[i]=T,sum[i]=1; 70 else sum[i]++; 71 if(t[r]!=T)t[r]=T,sum[r]=-1; 72 else sum[r]--; 73 } 74 if(t[0]!=T)t[0]=T,sum[0]=0; 75 LL ans=sum[0]+1; 76 re(i,1,len-1) 77 { 78 if(t[i]!=T)t[i]=T,sum[i]=0; 79 sum[i]+=sum[i-1];ans=ans*(sum[i]+1)%mod; 80 } 81 printf("%lld ",ans); 82 } 83 return 0; 84 }