我们可以通过绘制一些逻辑结论在O(n)的解决这个问题:由于所有的比赛都是一样的,也就是说,它们与字符串本身相匹配;从字符串索引i
开始的任何匹配将包含在i
(或长度允许的部分)之前开始的所有匹配。此外,任何长度大于其起始索引的匹配都将包含字符串起始部分到匹配起始部分的重复。我们只需要记录完整的匹配,我们可以在一次遍历的字符串中找到匹配,而不需要退后一步,并推导出其余的结果。
实例(非从零开始):
"aaaaaa":
Starting on index 2, we have a match length 5. This match necessarily includes
a match of length 4 starting on index 3 (since index 3 is index 2 for the
substring that starts on index 2). Continuing the same logic, we add 3 + 2 + 1
for a total of 15, without needing to scan and compare more than Substr2.
"aabaabaa":
Starting on index 2, we have a match length 1.
Starting on index 4, we have a match length 5. This match necessarily includes
a match of length 1 starting on index 5 (since index 5 is index 2 for the
substring that starts on index 4). It also necessarily includes a match of
length (5 - 3) starting on index 7 (since index 7 is index 4 for the substring
that starts on index 4), and this match implies another match of length 1,
starting on index 8. Altogether 1 + 5 + 1 + (5 - 3) + 1 = 10. Again, the scan
was O(n).
"aabaabaabaabaa":
Starting on index 2, we have a match length 1.
Starting on index 4, we have a match length 11.
1 + 11 + 1 + (11 - 3) + 1 + (8 - 3) + 1 + (5 - 3) + 1 = 31.
"aabaaab":
Starting on index 2, we have a match length 1.
For repeated patterns in the beginning of the string, we can use a formula
rather than multiple scans, so a string like "aabaaaaaaaaaab" would have the
same complexity as the one above, (number of times the pattern repeats - number
of times the pattern repeats in the beginning of the string) * total length of
repeated pattern at the start of the string. We identify a pattern if the
length of the first match is a multiple of its starting index. Identifying
this pattern and using the formula also prevents erroneously missing the
correct match to record (e.g., without it, we would have identified 'aa' and
'a' at the end as matches and missed the 'aab').
So starting on index 4, we have (3 - 2) * 2 = 2
Starting on index 5, we have a match length 3.
1 + 2 + 3 + 1 = 7
"ababcabab":
Starting on index 3, we have a match length 2.
Starting on index 6, we have a match length 4.
2 + 4 + 2 = 8
这看起来很'O(n^2)'给我...... – 2014-09-27 14:32:35