Boring counting
problem ' s Link:
http://acm.hdu.edu.cn/showproblem.php?pid=3518
Mean:
Give you a string that lets you find out how many substrings (no overlap) appear at least two times.
Analyse:
The use of the height array in the suffix array is rarely used in this array.
General idea: the idea of grouping statistics: The suffix of the same prefix is divided into a group, and then the ANS is counted for each fixed length of 1 to LEN/2.
First, we'll go through the suffix array and find the height array. The height array represents the length of the longest common prefix of the two suffixes of the dictionary next to each other (that is, the rank array is adjacent).
Since substrings cannot overlap, the range of substring lengths can be determined: 1~LEN/2.
Next we count the number of substrings of this length for each fixed length of the 1~LEN/2, which adds up to the answer.
The key is to understand the process of grouping statistics using the height array.
Time Complexity:o (NLOGN)
Source Code:
/** This code was made by crazyacking* verdict:accepted* submission date:2015-05-07-22.15* time:0ms* MEMORY:137KB*/#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<cmath>#include<string>#include<vector>#include<algorithm>#include<map>#include<Set>#defineMAXN 1000005#defineEPS 1e-8#defineINF 1<<30#defineZero (a) fabs (a) <epsusing namespacestd;//the following is a multiplication algorithm for the suffix arrayintWA[MAXN],WB[MAXN],WV[MAXN],WS[MAXN];intcmpint(RNintAintBintl) {returnr[a]==r[b]&&r[a+l]==r[b+l];}voidDaConst Char*r,int*sa,intNintm) { inti,j,p,*x=wa,*y=wb,*T; for(i=0; i<m;i++) ws[i]=0; for(i=0; i<n;i++) ws[x[i]=r[i]]++; for(i=1; i<m;i++) ws[i]+=ws[i-1]; for(i=n-1; i>=0; i--) sa[--ws[x[i]]]=i; for(j=1, p=1;p <n;j*=2, m=p) { for(p=0, i=n-j;i<n;i++) y[p++]=i; for(i=0; i<n;i++)if(SA[I]>=J) y[p++]=sa[i]-J; for(i=0; i<n;i++) wv[i]=X[y[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--) sa[--ws[wv[i]]]=Y[i]; for(t=x,x=y,y=t,p=1, x[sa[0]]=0, i=1; i<n;i++) X[sa[i]]=CMP (y,sa[i-1],sa[i],j)? p1:p + +; } return;}intSA[MAXN],RANK[MAXN],HEIGHT[MAXN];//finding the height arrayvoidCalheight (Const Char*r,int*sa,intN) { inti,j,k=0; for(i=1; i<=n;i++) rank[sa[i]]=i; for(i=0; i<n;height[rank[i++]]=k) for(k?k--:0, j=sa[rank[i]-1];r[i+k]==r[j+k];k++); return;}intSlove (intKintN) { intmmax=0, mmin=inf,ans=0; for(intI=1; i<=n;i++) { if(height[i]<k) {if(mmax-mmin>=k) Ans++; Mmax=0; mmin=inf; } Else{Mmax=max (Max (sa[i-1],sa[i]), Mmax); Mmin=min (min (sa[i-1],sa[i]), mmin); } } if(mmax-mmin>=k) ans++; returnans;}CharSTR[MAXN];intMain () { while(SCANF ("%s", str)!=eof&&strcmp (str,"#")) { intn=strlen (str); Da (str,sa,n+1, the); Calheight (Str,sa,n); intans=0; for(intI=1; i<=n/2; i++) ans+=Slove (i,n); printf ("%d\n", ans); } return 0;}
View Code
Suffix array---HDU 3518 boring counting