【definition】
[LCP] the full name of the longest common prefix, longest prefix between the two suffixes, we define the following
Meaning lcp (i, j) is the longest prefixes and suffixes i and j
Function [z] function z [i] represents the longest prefix and suffix i of the string
【Solve the problem】
Both were in solving this problem
I.e. seeking longest common prefix and suffix strings between suffixes and
But something is different,
Suffix array to finally determine that the longest common prefix lexicographic suffixes i-th and i + 1-th suffix
z functions to finally determine that the longest common prefix and suffix i of the string
Then find some other value by the longest common prefix
Learning algorithm []
Suffix array []
Suffix array can be obtained in the n-time lexicographically i-th and the i - 1 longest common prefix suffix
And this function is usually named height
Meaning height [i] is, for lexicographically the i - longest common prefix and a suffix of the suffix of the i-th
Is obtained has the following properties of: (s [i] denotes the i-th suffix)
1. If i is smaller than j, LCP (i, j) = min {LCP (k - 1, k), i + 1 <= k <= j}
LCP can take advantage of this demand with RMQ
2. Define H [i] as follows: i-th suffix starting in front of him and suffix LCP lexicographically
即: h [ i ] = height [ rank [ i ] ]
Then there are, for i> 1 and rank [i]> 1
h [ i ] > h [ i - 1 ] - 1 ;
Certify as follows:
And j is the first i - 1 suffix beginning to embark on the front of the suffix ranking position
Note: j is not the first i - 2
In this case, the suffix j-th and the i - 1 th in the LCP suffix defined height [rank [i - 1]], i.e. h [i - 1]
That is part of the right half of us to prove
Then we discuss j + 1 and i (obtained by the i - 1 + 1) of the relationship:
First, when j and i - 1 where letters are not equal, h [i - 1] is 0
Clearly then h [i]> h [i - 1] - 1
Second, when j and i - 1 is equal to the first letter of the case,
Clearly then, j, and i - LCP 1 is h [i - 1] - 1
In the suffix ranking than i exam, and the suffix i LCP's longest, highest similarity is clearly in SA recently that away from him
Immediate SA [rank [i - 1]] - 1
That is, h [i]> = h [i - 1] - 1
QED
So every time we find the longest prefix when are available from h [i - 1] begin retrieving
Is analogous manacher
code show as below:
void GetHeight() { int j, k = 0; for(int i = 1; i <= N; i++) { if(k) k--; int j = sa[rak[i] - 1]; while(s[i + k] == s[j + k]) k++; Height[rak[i]] = k; printf("%d\n", k); } }
topic:
[SDOI 2008] Sandy card