Reputation: 277
I need to solve problem - find longest substring in two words with suffix tree. I built suffix for first and secod word, but how can I find longest substring in two words? Could you suggest a possible algorithm for solving this problem?
Upvotes: 1
Views: 283
Reputation: 2777
The trick is to use a single suffix tree for both words:
First use some non-strings character like $
or #
or something (must not be part of any string) to join strings
i.e. strings abra
and abracadabra
get joined to abra$abracadabra#
Then build suffix tree from that.
Now from leaves ending with $
climb up and mark nodes as part of word1
Do the same for leaves ending with #
, marking them as part of word2
Now we can do simple DFS
traversal from root, as longest sub-string will be some path from root (only checking nodes that are part of both words)
Complexity - O(a+b)
(suffix tree building (if build fast way) + O(a+b)
(dfs) = O(a+b)
Upvotes: 1