中的所有重复模式我有一个问题,我必须找到句子中存在的所有重复模式。查找段落
例子:'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