中的所有重複模式我有一個問題,我必須找到句子中存在的所有重複模式。查找段落
例子:'camel horse game camel horse gym camel horse game' # This is the sanitized string as I will cleanup anything other than words before it.
['camel horse game', 0, 3, 6] # pattern and Index where it is repeated
['camel horse', 0, 3, 6] # Another pattern, let it be a substring of the previous pattern
後綴樹是一種很好的解決方案,但我無法理解如何實現它的話,而不是字母/字符?
使用標準Duplicate Substringss solution
將無法正常工作,因爲它會找到帶有一半/半字的模式。 - >'camel horse', 'amel hor' .... 'am h'
這幾乎沒有任何用處。
在此先感謝。
如果你可以用一些例子(任何語言)解釋它,或者通過支持答案可以拋出更多光的僞代碼,那將是非常好的。 –
我有疑問,如果我有超過26個不同的單詞,那麼我將不得不創建字母組合,那麼在這種情況下它將不會是可持續/可擴展的解決方案。 –
有許多算法(Farach的算法是第一個和更容易理解的算法之一),用於在字符串由整數值組成的情況下構建後綴樹。您可以爲每個單詞分配一個數字值,然後從這些數字中構建後綴樹。這是一個非常棘手的算法來編碼自己 - 就像任何用於構建後綴樹的算法一樣 - 但如果你想走這條路線,這可能是最優雅的方法。 – templatetypedef