Continuous repeating substring problem
POJ 2406 Power Strings
http://poj.org/problem?id=2406
Ask if a string can be written in this form of A^n.
Although this problem is more appropriate to use kmp, but we still use the suffix array to do, the ability to consolidate the suffix array.
For a string, if can write a^n this form, we can violently enumerate the cyclic section length L, then the suffix suffix (1) and suffix (1 + L) LCP should be lenstr-l. If it can be fulfilled, that is, no, it is Not.
The DA algorithm or time-out, and so I learned DC3 to write it up.
In fact, this problem can not be enumerated, considering that if it can be written as a^n this form, then the length of its circulation section must be lenstr-height[rank[1]]
It might be clearer to give a picture.
If it is a follow-up link, then height[rank[1] is the 1th position before and after the lcp, it must be the second cycle section There.
And then we can judge the Violence. ~dc3, 2750ms to get past it.
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;#defineINF (0x3f3f3f3f)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN =3*1000000+ -;Const intN =maxn;#defineF (x) ((x)/3+ ((x)%3==1?0:tb))#defineG (x) ((x) <tb? ( X) *3+1: (((x)-tb) *3+2)intr[maxn];intwa[maxn],wb[maxn],wv[maxn],ws[maxn];intsa[maxn];intC0 (int*r,intAintB) {returnr[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];}intC12 (intKint*r,intAintB) {if(k==2)returnr[a]<r[b]| | R[A]==R[B]&&C12 (1, r,a+1, B +1); Else returnr[a]<r[b]| | r[a]==r[b]&&wv[a+1]<wv[b+1];}voidSortint*r,int*a,int*b,intNintM) {inti; for(i=0; i<n; I++) wv[i]=r[a[i]]; for(i=0; i<m; I++) ws[i]=0; for(i=0; i<n; I++) ws[wv[i]]++; for(i=1; i<m; I++) ws[i]+=ws[i-1]; for(i=n-1; i>=0; I--) b[--ws[wv[i]]]=a[i]; return;}voidDC3 (int*r,int*sa,intNintM) {//same meaning as Da intI,j,*rn=r+n,*san=sa+n,ta=0, tb= (n+1)/3, tbc=0, p; r[n]=r[n+1]=0; for(i=0; i<n; I++)if(i%3!=0) wa[tbc++]=i; Sort (r+2, wa,wb,tbc,m); Sort (r+1, wb,wa,tbc,m); Sort (r,wa,wb,tbc,m); for(p=1, Rn[f (wb[0])]=0, i=1; i<tbc; i++) rn[f (wb[i])=C0 (r,wb[i-1],wb[i])? P1:p + +; if(p<Tbc) DC3 (rn,san,tbc,p); Else for(i=0; i<tbc; I++) san[rn[i]]=i; for(i=0; i<tbc; I++)if(san[i]<tb) wb[ta++]=san[i]*3; if(n%3==1) wb[ta++]=n-1; Sort (r,wb,wa,ta,m); for(i=0; i<tbc; I++) wv[wb[i]=g (san[i])]=i; for(i=0, j=0, p=0; I<ta && j<tbc; p++) sa[p]=C12 (wb[j]%3, r,wa[i],wb[j])? wa[i++]:wb[j++]; for(; i<ta; P++) sa[p]=wa[i++]; for(; j<tbc; P++) sa[p]=wb[j++]; return;}intrank[maxn], height[maxn];voidCalheight (int*r,int*sa,intN) {//here n is the actual length inti,j,k=0;//the legal range of height[] is 1-n, where 0 is the end-added character for(i=1; i<=n; I++) rank[sa[i]]=i;//rank according to SA for(i=0; i<n; height[rank[i++]] = K)//definition: h[i] = height[rank[i]] for(k?k--:0, j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);//optimize the calculation height process according to h[i] >= h[i-1]-1}Charstr[maxn];voidwork () {intLenstr =strlen (str); for(inti =0; I < lenstr; ++i) r[i] =str[i]; r[lenstr]=0; DC3 (r, sa, lenstr+1, -); Calheight (r, sa, lenstr); intt = lenstr-height[rank[0]]; if(t = = Lenstr | | lenstr% t! =0) {printf ("1\n"); } Else { for(inti = t; I < lenstr; i + =T) { for(intj =0; J < t; ++J) {if(str[j]! = Str[i +j]) {printf ("1\n"); return; }}} printf ("%d\n", lenstr/t); }}intmain () {#ifdef local freopen ("Data.txt","R", stdin);#endif while(SCANF ("%s", str)! =EOF) { if(str[0] =='.') break; Work (); } return 0;}
View Code
Poj 2406 Power Strings suffix Array solution