【发布时间】:2018-07-29 22:18:18
【问题描述】:
我正在寻找一种能够在字符串中找到最重复序列的算法(可能在 Python 中实现)。对于 REPETITIVE,我的意思是任何字符的组合,一遍又一遍地重复而不中断(串联重复)。
我正在寻找的算法不与“找到最常用的单词”算法相同。实际上,重复块不需要是字符串中最常见的词(子串)。
例如:
s = 'asdfewfUBAUBAUBAUBAUBAasdkBAjnfBAenBAcs'
> f(s)
'UBAUBAUBAUBAUBA' #the "most common word" algo would return 'BA'
不幸的是,我不知道如何解决这个问题。非常欢迎任何帮助。
更新
一个额外的例子来说明我希望返回重复次数最多的序列,无论其基本构建块是什么。
g = 'some noisy spacer'
s = g + 'AB'*5 + g + '_ABCDEF'*2 + g + 'AB'*3
> f(s)
'ABABABABAB' #the one with the most repetitions, not the max len
来自@rici 的示例:
s = 'aaabcabc'
> f(s)
'abcabc'
s = 'ababcababc'
> f(s)
'ababcababc' #'abab' would also be a solution here
# since it is repeated 2 times in a row as 'ababcababc'.
# The proper algorithm would return both solutions.
【问题讨论】:
-
重复,是指至少出现两次的子串吗?
-
看看后缀树和 Ukkonen 算法
-
重复次数最长还是长度最长?
-
@Oliver 最长重复。
标签: python regex python-3.x string algorithm