java查找不重叠的重复子字符串
我的问题类似于寻找重复时间最长的非重叠子字符串,但以这种方式。例如,我的输入数组是0,0,0,1,2,1,1,2,1,1,2,1,2,0,0
此输入中最长的重复和不重叠子字符串是121
,计数为3
现在,从输入数组中删除出现的121
,并用-1
替换它们
我的输入现在变成:0,0,0,-1,-1,-1,2,0,0
现在再次查找相同的子字符串
注:-1不应包含在答案中。因此,下一个最长的非重叠和重复子字符串是00
,计数为2
从输入中删除出现的0,0,然后变成:0,-1,-1,-1,2,-1
现在唯一的引用是长度为1的0和2。所以,我们到此为止
我只能想到暴力是O(n3)。也尝试过后缀树,但不知道如何在其中包含非重叠条件
我非常天真的实现:
因为我希望子字符串的计数至少为2,所以它的最大长度可以为N/2(N是输入数组的大小)
for len = N/2 to 1:
// find all sub-strings of length as "len" in the input sequence
for i=0 to N-len+1
// the sub-string is s[i... (i+len)]
// find non-overlapping count of this sub-string in the input sequence using KMP
int count = find_Count();
// take the substring with maximum count, let it be "r"
end
modify the array to place replace non-overlapping occurrences of "r" with -1
end
正如你在上面看到的,复杂性是O(N3)
任何最佳解决方案、提示或解释都会有所帮助
谢谢
共 (0) 个答案