![]() |
VOOZH | about |
Prerequisites :
The Longest Common Extension (LCE) problem considers a string s and computes, for each pair (L , R), the longest sub string of s that starts at both L and R. In LCE, in each of the query we have to answer the length of the longest common prefix starting at indexes L and R.
Example:
String : “abbababba”
Queries: LCE(1, 2), LCE(1, 6) and LCE(0, 5)Find the length of the Longest Common Prefix starting at index given as, (1, 2), (1, 6) and (0, 5).
The string highlighted “green” are the longest common prefix starting at index- L and R of the respective queries. We have to find the length of the longest common prefix starting at index- (1, 2), (1, 6) and (0, 5).
In Set 1, we explained about the naive method to find the length of the LCE of a string on many queries. In this set we will show how a LCE problem can be reduced to a RMQ problem, hence decreasing the asymptotic time complexity of the naive method.
Reduction of LCE to RMQ
Let the input string be S and queries be of the formLCE(L, R). Let the suffix array for s be Suff[] and the lcp array be lcp[].
The longest common extension between two suffixes SL and SR of S can be obtained from the lcp array in the following way.
Proof: Let SL = SL...SL+C...sn and SR = SR...SR+c...sn, and let c be the longest common extension of SL and SR(i.e. SL...SL+C-1 = sn...SR+c-1). We assume that the string S has a sentinel character so that no suffix of S is a prefix of any other suffix of S but itself.
Therefore we have c = lcp[i]
Thus we have reduced our longest common extension query to a range minimum-query over a range in lcp.
Algorithm
The minimum value is the length of the LCE for that query.
Implementation
LCE (1, 2) = 1 LCE (1, 6) = 3 LCE (0, 5) = 4
Analysis of Reduction to RMQ method
Time Complexity :
Hence the overall time complexity is O(N.logN + Q. (|invSuff[R] – invSuff[L]|))
where,
Q = Number of LCE Queries.
N = Length of the input string.
invSuff[] = Inverse suffix array of the input string.
Although this may seems like an inefficient algorithm but this algorithm generally outperforms all other algorithms to answer the LCE queries.
We will give a detail description of the performance of this method in the next set.
Auxiliary Space: We use O(N) auxiliary space to store lcp, suffix and inverse suffix arrays.