Finding longest common substring using Trie

北战南征 提交于 2019-12-10 23:27:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!