问题
How can i find LCS (longest common substring) among two or more strings using trie ?
I have an idea like this - suppose my first string is "abbcabdd". then i will first insert "abbcabdd" in trie ,then "bbcabdd", then "bcabdd" .... , then "d" and repeat this process for every string .
Then calculate the longest substring by traversing the trie.
but i think it is not efficient. Is there any other improved method ?
回答1:
What you are describing is exactly a suffix tree - Use an algorithm optimized to generate a suffix tree, and you will get your efficiency increased!
Note that there is an algorithm for building a suffix tree in O(n)
(assuming a constant alphabet, of course).
来源:https://stackoverflow.com/questions/12924595/finding-longest-common-substring-using-trie