For repetitions, if the length of the repeating substring is determined Len, the number of repetitions K=LCP (Start,start+len)/len+1. The complexity of the violent enumeration of start and Len is O (n^2), unacceptable. And there is a rule that if we only enumerate the integer multiples of Len as the starting point, if we move it forward less than the Len bit can form a longer string, then that position is P=start-len+lcp%len. So each time we calculate both and ask Max to do Max again with ans.
#include <cstdio>#include<string>#include<iostream>#include<Set>#include<algorithm>#include<vector>#include<map>#include<cstring>#include<queue>#defineLL intusing namespacestd;ConstLL N =100055;classsf{//N: Array size Public: intX[n], y[n], c[n]; intHeight[n], Str[n], sa[n], rank[n];//The height array starts at 2 and the SA record rank=i subscript intSlen; intm =1050;//Character Set processing size (passing in if not a number, need to do displacement conversion) BOOLcmpint* R,intAintBintl) {returnR[a] = = R[b] && r[a + l] = = R[b +L]; } voidSuffix (intN) {++N; intI, J, p; for(i =0; I < m; ++i) C[i] =0; for(i =0; I < n; ++i) C[x[i] = str[i]]++; for(i =1; I < m; ++i) C[i] + = c[i-1]; for(i = n-1; 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 = n-1; 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]]= CMP (y, Sa[i-1], Sa[i], j)? P-1: p++; } if(P >= N) Break; M=p; } intK =0; N--; for(i =0; I <= N; ++i) Rank[sa[i] =i; for(i =0; I < n; ++i) {if(k)--K; J= Sa[rank[i]-1]; while(Str[i + K] = = Str[j + K]) + +K; Height[rank[i]]=K; //cout << k << Endl; } } Static Const intBitlen = -; ll Lg2 (ll P)//calculate log2 (n) { return(LL) (log (P)/log (2)); } LL Dp[bitlen][n]; LL Bit[bitlen]; voidINITRMQ ()//Initialize{bit[0] =1; for(inti =1; i < Bitlen; i++) Bit[i] =2* Bit[i-1]; for(inti =0; I <= Slen; i++) dp[0][i] =Height[i]; dp[0][0] = dp[0][1] =0; for(LL i =1; Bit[i] < Slen +1; i++) for(LL j =0; J + Bit[i] <= Slen +1; J + +) Dp[i][j]= Min (Dp[i-1][J], Dp[i-1][j + Bit[i-1]]); } ll query (ll L, ll R)//Query the LCP between two rank { if(r = = L)returnSlen-Sa[l]; if(L >r) Swap (L, R); L++; LL MiG= LG2 (R-l +1.0); returnMin (Dp[mig][l], Dp[mig][r-bit[mig] +1]); } voidInitstring&s) {Slen=s.size (); for(inti =0; i < Slen; i++) Str[i]= S[i]-'a'+2;//if it is a character, it is mapped to a sequence starting from 1Str[slen] =1;//1 as Terminator to prevent cross-borderSuffix (Slen); INITRMQ (); } intsolve () {intAns =0; for(intLen =1; Len <= Slen; len++) { for(inti =0; i < Slen; i+=Len) { intR1 = Rank[i], r2 = rank[i+Len]; intLCP =query (r1, R2); Ans= Max (ans, Lcp/len +1); if(I-len + Lcp%len >=0) {LCP= Query (Rank[i-len + Lcp%len], rank[i + lcp%Len]); Ans= Max (ans, Lcp/len +1); } } } returnans; }}SF;intMain () {Cin.sync_with_stdio (false); strings; while(Cin >>s) {sf.init (s); cout<< sf.solve () <<Endl; } return 0;}
hihocoder-1419 suffix array four • Repeat Melody 4 for the substring with the most continuous repetition times