Consider the following algorithm, where:
str is an event string
T(i) is the suffix tree for the substring str(0..i) .
T(i+1) quickly obtained from T(i) , for example, using this algorithm
For each position of the character i in the input string str , cross a path starting at the root of T(i) along the edges marked with consecutive characters from the input, starting at position i + 1 .
This path defines a repeating line. If the path is longer than the previously found paths, write the new maximum length and position i + 1 .
Update the suffix tree with str [i+1] and repeat for the next position.
Something like this pseudo code:
max.len = 0 max.off = -1 T = update_suffix_tree (nil, str [0]) for i = 1 to len (str) r = root (T) j = i + 1 while j < len (str) and r.child (str [j]) != nil r = r.child (str [j]) ++j if j - i - 1 > max.len max.len = j - i - 1 max.off = i + 1 T = update_suffix_tree (T, str [i+1])
In the k th iteration, the internal while is performed in no more than n - k iterations, and the construction of the suffix tree is O(k) , therefore, the complexity of the body of the cycle is O(n) , and it performed n-1 times, therefore the whole complexity of the O(n^2) .
chill source share