Find the double longest suffix in linear time

advertisements

Given a string s, find the longest double suffix in time complexity O(|s|).

Example: for string banana, the LDS is na. For abaabaa it's baa.

Obviously I thought about using a suffix tree, but I'm having trouble to find double suffix in it.


Reverse the string and build sparse array P[i][j], where i is from 0 to log(n), j is from 0 to n-1, n is the length of the string. P[i][j] refers to the rank of the suffix starting from position j and length 2^i. So if P[i][j]=P[i][k], the first 2^i chars of the suffixes at indexes j and k are equal.

Now your problem reduces to finding a Longest Common Prefix for 0(start of the reversed string) and another suffix at index i, such that LCP >= i. Where LCP can be computed by simply using P array in log(n) time, by comparing first 2^x chars of these two suffixes and gradually reducing x.

Total complexity is n*log(n)*log(n). Here is the working C++ source code: https://ideone.com/aJCAYG