An array of SA suffixes
First of all make sure that \ (sa\) is something
\ (sa[i]\) indicates which suffix is ranked as \ (i\)
As for the suffix \ (i\) ranking is how much, that is \ (rank[i]\)
Of course
The most difficult thing to understand is the cardinal sort
If you do not use the cardinal sort, each time for a two-tuple direct \ (sort\)
This complexity is \ (O (nlog^2) \)
This should be done for a two-tuple base order:
First, all elements are dropped into the corresponding bucket in the last dimension.
Then take it out in sequence
And then drop them in the first dimension.
Take it out again.
So you can sort it out.
Throw the code out first.
BOOLcmpintIintJintK) {returnY[I]==Y[J]&&Y[I+K]==Y[J+K];}voidGetsa () {intm= -; for(intI=1; i<=n;++i) t[x[i]=a[i]]++; for(intI=1; i<=m;++i) T[i]+=t[i-1]; for(inti=n;i>=1; i.) sa[t[x[i]]--]=i; for(intk=1; k<=n;k<<=1) {intp=0; for(intI=0; i<=m;++i) y[i]=0; for(intI=n-k+1; i<=n;++i) y[++p]=i; for(intI=1; i<=n;++i)if(sa[i]>k) Y[++p]=sa[i]-k; for(intI=0; i<=m;++i) t[i]=0; for(intI=1; i<=n;++i) t[x[y[i]]]++; for(intI=1; i<=m;++i) T[i]+=t[i-1]; for(inti=n;i>=1;----) sa[t[x[y[i]]]--]=y[i]; Swap (x, y); x[sa[1]]=p=1; for(intI=2; i<=n;++i) x[sa[i]]=cmp (sa[i],sa[i-1],k)? p:++p;if(p>=n) Break; M=p; }}
First, the first time to do \ (k=0\)
The second dimension equivalent to each suffix is the same.
So, just follow the first dimension (i.e. your own value)
Do a cardinal sort once
Next
Each base order takes advantage of the last value
Remember, the Cardinal sort is to start with the second dimension from small to big.
So, let's get the second dimension in order first.
First of all, the smallest must be something without a second dimension.
So let's just throw these numbers right into the array.
And then there's the second-dimensional thing.
What is the second dimension of the first \ (i\) bit? \ (rank[i+k]\)
So, the enumeration \ (Sa\)is reached from childhood, so that the second dimension is guaranteed to grow from small to large
So, as long as \ (sa[i]>k\)
It proves to be the second dimension of a thing.
So, throw \ (sa[i]-k\) inside the array.
In that case, take the second dimension and shoot it.
And then drop it in the bucket in the first dimension.
Just do the cardinal sort again.
This will be able to find out \ (sa\)
It looks very simple, eh.
Just don't confuse the array.
Be sure to figure out what each array is doing.
Like my Code.
\ (sa\) is the suffix array,\ (sa[i]\) indicates which of the strings ranked \ (i\)
\ (rank\) equivalent to rank,\ (rank[i]\) represents the rank of the first ( i\) string
\ (x,y\) two arrays are in record order
The order of ordering of first and second dimensions is recorded separately
\ (t\) is a bucket
So we'll be happy to find out \ (sa\)
There is also an array \ (height\)
\ (height[i]\) indicates the length of the longest public prefix of string \ (sa[i]\) and \ (sa[i-1]\)
For example, the longest public prefix of the suffix \ (i\) and \ (j\) is now required
Then just ask \ (min (height[i]), I \in [rank[i]+1,rank[j]]\)
Because it's already sorted in the dictionary order.
\ (height\) obviously can be violent beg
But it's not beautiful.
We have \ (height[rank[i]]>=height[rank[i-1]]-1\)
Proof (from \ (hihocoder\))
Set \ (suffix (k) \) is the suffix of the previous name (suffix (i-1) \ ),
Their longest common prefix is \ (height[rank[i-1]]\)
Then \ (suffix (k+1) \) will be in front of \ (suffix (i) \) (here requires \ (height[rank[i-1]]>1\), if \ (height[ rank[i-1]]≤1\), the original form clearly established)
and the longest common prefix of \ ( suffix (k+1) \) and \ (suffix (i) \) is \ (height[rank[i-1]]-1\),
So \ (suffix (i) \) and the longest common prefix of the suffix in its previous name is at least \ (height[rank[i-1]]-1\)
Well, we'll do it in the order of \ (rank\ ) (height\) .
for(int i=1;i<=n;++i)Rank[SA[i]]=i; for(int i=1,j=0;i<=n;++i) { if(j)j--; while(a[i+j]==a[SA[Rank[i]-1]+j])++j; height[Rank[i]]=j; }
I'm not very familiar now.
I'll do more humorous later.
An array of SA suffixes