The suffix array sa (x) represents the position of the X-bit before sorting after sorting.
There are two kinds of this thing, one is multiplication, the time complexity O (n log n) or O (n log2n), and the other is O (n) which does not know what method to do.
As for what the second method is, and the wrong person does not know, so just say multiply.
Consider a normal comparison of two strings, all from the beginning to the end:
Then, if the two strings are broken in half, and each paragraph is known to rank, it is equivalent to the first paragraph as the first keyword, the second paragraph for the second keyword sort.
According to this nature, it can be thought that if you first sort the string with a substring starting at one of each position, you can sort the substring with a length of two at a time of up to n log n.
↑ Probably long like this, note the last to fill an empty character.
And so on, you can multiply this to find the suffix of the order, or to pay attention to the last fill empty characters.
If you sort by cardinality, the time complexity for each sort is O (n), then the total complexity is O (n log n).
If you use the quick sort, the total complexity is O (n log2n), the heart has a party constant minimum to live.
#include <iostream>#include<iomanip>#include<cmath>#include<cstring>#include<cstdio>#include<cstdlib>#include<algorithm>#include<queue>#defineMAXN 2000010using namespaceStd;inlineintRead () {intxx=0, ff=1; CharCh=GetChar (); while(IsDigit (ch) = =0&&ch!='-') ch=GetChar (); if(ch=='-') ff=-1, ch=GetChar (); while(IsDigit (CH)) xx=xx*Ten+ch-'0', ch=GetChar (); returnxx*ff;}voidWriteintx) { intHfS0;Charch[ the]; if(x<0) {x=-x; Putchar ('-'); } while(x) ch[++ff]= (%Ten)+'0', x/=Ten; if(ff==0) Putchar ('0'); while(FF) Putchar (ch[ff--]); Putchar (' ');}structsa{intsa[maxn],ord[maxn],x[maxn],n,m; intY[MAXN],C[MAXN]; CharS[MAXN]; voidstart () {scanf ("%s", s); N=strlen (s); m= the; } voidS_sort () {memset (x,0,sizeof(x)); memset (Y,0,sizeof(y)); Memset (c,0,sizeof(c)); for(intI=0; i<n;i++) c[x[i]=s[i]]++; for(intI=1; i<m;i++) c[i]+=c[i-1]; for(inti=n-1; i>=0; i--) sa[--c[x[i]]]=i; for(intk=1; k<=n;k<<=1) { intp =0; for(inti=n-k;i<n;i++) y[p++] =i; for(intI=0; i<n;i++)if(Sa[i] >= k) y[p++] = Sa[i]-K; for(intI=0; i<m;i++) C[i] =0; for(intI=0; i<n;i++) c[x[y[i]]]++; for(intI=1; i<m;i++) C[i] + = c[i-1]; 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])? p1:p + +; if(p>=n) Break; M=p; } } voidprint () { for(intI=0; i<n;i++) write (Sa[i]+1); }}t;intMain () {T.start (); T.s_sort (); T.print (); return 0;}
does not require constant optimization.
#include <iostream>#include<iomanip>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#defineMAXN 1000010using namespacestd;intRead () {intHfS1, x=0;CharCh=GetChar (); while(IsDigit (ch) = =0&& ch!='-') ch=GetChar (); if(ch=='-') ff=-1, ch=GetChar (); while(IsDigit (CH)) x=x*Ten+ch-'0', ch=GetChar (); returnx*ff;}voidWriteintx) { intHfS0;Charch[ the]; while(x) ch[++ff]= (%Ten)+'0', x/=Ten; while(FF) Putchar (ch[ff--]); Putchar (' ');} CharS[MAXN];BOOLF[MAXN];intrnk[maxn*2],ord[maxn*2],tp[maxn*2],n,c;//tmp (x) is the number of digits in X, and Ord (x) is ranked in the firstvoidinit () {scanf ("%s", s); N=strlen (s); for(intI=1; i<=n;i++) Ord[i]=int(s[i-1]), rnk[i]=i; for(inti=n+1; i<=n*2; i++) Ord[i]=0;}BOOLcmpintAintb) { returnord[a]==ord[b]?ord[a+c]<ord[b+c]:ord[a]<ord[b];}voidprint () { for(intI=1; i<=n;i++) {write (rnk[i]); } printf ("\ n"); }voidcpy () { for(intI=1; i<=n;i++) {Ord[i]=Tp[i]; }}voidS_sort () {intyes=0; c=0; Do{sort (rnk+1, rnk+n+1, CMP);inttmp=1; //print (); for(intI=1; i<=n;i++) {Tp[rnk[i]]=tmp;yes=tmp; TMP+ = (ord[rnk[i]]==ord[rnk[i+1]]&&ord[rnk[i]+c]==ord[rnk[i+1]+c])?0:1; } cpy (); if(c!=0) c*=2; ElseC=1; } while(yes<n);} intMain () {init (); S_sort (); Print (); return 0;}
the non-constant optimization will be T's
And something wrong with the suffix array