Portal: Repeated substrings
Test instructions: Given a string, the number of different substrings that are repeated at least once.
Analysis: The simulation writes out the substring suffix side-by good order can be found, each occurrence of a new number of repeating substring is the current height value minus the previous height value.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespacestd;Const intMAXN =100010; typedefLong LongLL;intSA[MAXN];intT1[MAXN], T2[MAXN], C[MAXN];intRAN[MAXN], HEIGHT[MAXN];intS[MAXN];CharSTR[MAXN];voidBuild_sa (intS[],intNintm) {intI, J, p, *x = t1, *y =T2; for(i =0; I < m; i++) C[i] =0; for(i =0; I < n; i++) C[x[i] = s[i]]++; for(i =1; I < m; i++) C[i] + = c[i-1]; for(i = n1; I >=0; i--) Sa[--c[x[i]] =i; for(j =1; J <= N; J <<=1) {p=0; for(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 < m; i++) C[i] =0; for(i =0; I < n; i++) c[x[y[i]]]++; for(i =1; I < m; i++) C[i] + = c[i-1]; for(i = n1; I >=0; i--) sa[--c[x[y[i] []] =Y[i]; Swap (x, y); P=1, x[sa[0]] =0; for(i =1; I < n; i++) X[sa[i]]= y[sa[i-1] [= Y[sa[i]] && y[sa[i-1]+J] = = Y[sa[i]+j]? P1: p++; if(P >= N) Break; M=p; }}voidGetHeight (intS[],intN) {intI, j, k =0; for(i =0; I <= N; i++) Ran[sa[i]]=i; for(i =0; I < n; i++) { if(k) k--; J= sa[ran[i]-1]; while(S[i+k] = = S[j+k]) k++; Height[ran[i]]=K; }}intMain () {intT; scanf ("%d", &T); while(t--) {scanf ("%s", str); intn =strlen (str); for(inti =0; I <= N; i++) S[i]=Str[i]; Build_sa (s, n+1, -); GetHeight (s, N); LL ans=0; for(inti =1; I <= N; i++) { if(height[i]>height[i-1]) ans+=height[i]-height[i-1]; } printf ("%lld\n", ans); } return 0;}
View Code
Uvalive 6869 (suffix array)