Title Link: hdu_3518_boring counting
Test instructions
Give you a string that lets you find the number of strings that don't overlap and appear more than 1 times
Exercises
The suffix array is applied to the height array, we enumerate the length of the string, and then segment the height array, matching the conditions on the ans++
Why do this because the height array has the maximum number of prefixes for adjacent rank suffixes, and if the value of height is greater than or equal to the length of our enumeration Len,
Then it is possible that this segment exists with more than two strings of that length, and then we count the length of the beginning of this segment L and the end length R, if R-l>=len, stating that this paragraph must have
More than one of the same substrings of that length, because we are enumerating Len each time, by the segment of Len, so it does not repeat.
1#include <bits/stdc++.h>2 #defineF (I,A,B) for (int i=a;i<=b;++i)3 using namespacestd;4 5 namespacesuffixarray{6 #defineFN (n) for (int i=0;i<n;i++)7 Const intN =1e3+7;8 intRnk[n],sa[n],height[n],c[n];CharS[n];9 voidGetsa (intNintMint*X=RNK,int*y=height) {TenFN (m) c[i]=0; FN (n) c[x[i]=s[i]]++; FN (m) c[i+1]+=C[i]; One for(inti=n-1; i>=0; i--) sa[--c[x[i]]]=i; A for(intk=1, p;p=0, K<=n;k=p>=n? n:k<<1, m=2) { - for(inti=n-k;i<n;i++) y[p++]=i; -FN (N)if(sa[i]>=k) y[p++]=sa[i]-K; theFN (m) c[i]=0; FN (n) c[x[y[i]]]++; FN (m) c[i+1]+=C[i]; - for(inti=n-1; i>=0; i--) sa[--c[x[y[i]]]]=Y[i]; -Swap (x, y), p=1, x[sa[0]]=0; - for(intI=1; i<n;i++) +x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p + +; - } +FN (n) rnk[sa[i]]=i; A for(intI=0, j,k=0; i<n-1; height[rnk[i++]]=k) at for(k=k?k-1: k,j=sa[rnk[i]-1];s[i+k]==s[j+k];k++); - } - } - -InlinevoidUpdint&a,intb) {if(a>b) a=b;} -InlinevoidUpuint&a,intb) {if(a<b) a=b;} in - using namespaceSuffixarray; to intMain () { + while(~SCANF ("%s", s)) - { the if(s[0]=='#') Break; * intLen=strlen (s), ans=0; $Getsa (len+1, -);Panax NotoginsengF (I,1, Len) - { the intl=len+1, r=0; +F (J,1, Len) A { the if(height[j]>=i) upd (L,sa[j]), UPU (R,sa[j]); + Else - { $ if(r-l>=i) ans++; $L=r=Sa[j]; - } - } the if(r-l>=i) ans++; - }Wuyiprintf"%d\n", ans); the } - return 0; Wu}View Code
hdu_3518_boring counting (suffix array)