Given a string
s, find the longest double suffix in time complexity
Example: for string
banana, the LDS is
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
i is from
j is from
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
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
Total complexity is
n*log(n)*log(n). Here is the working C++ source code: https://ideone.com/aJCAYG