我手头有一个问题,我必须找到句子中存在的所有重复模式。
示例:'camel horse game Camel Horse Gym Camel Horse Game' # 这是经过 sanitizer 的字符串,因为我将清理除前面的单词以外的任何内容。
['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
后缀树是一个很好的解决方案,但我无法理解如何针对单词而不是字母/字符实现它?
使用标准重复子字符串解决方案
将不起作用,因为它会找到带有缺口/半字的模式。 -> 'camel horse', 'amel hor' .... 'am h'
这实际上没有任何用处。
提前致谢。
最佳答案
您可以为您想要的任何字母表构建后缀树。想象一下,您创建了一个字母表,其中段落中的每个不同单词都被视为单个字母。然后,后缀树将让您找到段落中重复的单词序列,而无需将单词分解为单个字符。
关于algorithm - 查找段落中的所有重复模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40206666/