Suffix Arrays are a powerful tool for processing strings. Suffix Arrays can solve most suffix trees. Because they are easier to implement than suffix trees, they are favored by ACM fans, of course there are still some problems that can only be solved by the suffix tree. after learning the suffix tree, I will add it. Suffix Arrays are most commonly used to obtain the longest common prefix.
Suffix array, suffix [I] indicates the suffix starting from the I character;
Suffix array SA, retain 1 ~ A certain arrangement of N ensures that suffix [SA [I] <suffix [SA [I] can prove that any suffixes starting from different positions cannot be equal, and it is not proved here.
Noun array: rank [I] Stores suffix [I] the ranking of all suffixes from small to large. Simply put, the suffix array is "Who is the number ?", The number of places in the ranking array is "what do you rank ?". It is easy to see that Suffix Arrays and ranking arrays are inverse operations.
Height array, height [I] indicates the longest public prefix of suffix [SA [I-1] and suffix [SA [I], which is the longest public prefix in practice, use it to obtain the necessary things.
Suffix Arrays can solve the following problems:
I. single string problems:
(1) repeated strings
Example 1: Maximum repeated substrings that can be overlapped
For a given string, obtain the longest duplicate substring. The two substrings can overlap.
Algorithm: You only need to find the maximum height.
Example 2: Maximum repeated substrings (PKU 1743) that cannot overlap)
For a given string, obtain the longest duplicate substring. The two substrings cannot overlap.
Algorithm: Binary answer, which becomes a criterion. Binary answerCodeAnd determine the part of the code, I personally think the most important part:
Int Le = 0, rI = NM-1;
While (Le <RI)
{
Int mid = (le + RI + 1)/2;
If (OK (MID, nm) Le = mid;
Else rI = mid-1;
}
The final result is Le + 1;
Int OK (INT Len, int nm)
{
Int I, MX = 0, MI = Nm;
For (I = 0; I <Nm; I ++)
{
If (H [I] <Len) {MX = 0; MI = Nm ;}
Else
{
MX = max (MX, sa [I]);
MX = max (MX, sa [I + 1]);
Mi = min (MI, sa [I]);
Mi = min (MI, sa [I + 1]);
}
If (Mx-mi> Len) return 1;
}
Return 0;
}
Example 2: the maximum number of duplicate substrings that can overlap K times (PKU 3261)
Given a string, find the longest repeated substring that appears at least K times. The K substrings can overlap.
Or two points.
Code for determining the part:
Int appk (int x, int K, int N)
{
Int APP = 1, I;
For (I = 0; I <n; I ++)
{
If (H [I]> = X)
{
APP ++;
If (APP = K)
Return 1;
}
Else
APP = 1;
}
Return 0;
}