[英]KMP pattern match algorithm time complexity can be O(m*n)?
I have seen few answers here, but still need some clarification. 我在这里看到的答案很少,但仍需要一些澄清。 KMP has
O(n+m)
worst case time complexity. KMP具有
O(n+m)
最差情况时间复杂度。 But what I understood, in worst case scenario it should be O(n*m)
. 但据我所知,在最坏的情况下它应该是
O(n*m)
。 Lets take an example: 让我们举一个例子:
string(n): aaaaaaaaa
pattern(m): aaab
First 3 characters match. 前3个字符匹配。 Then there is a mismatch.
然后是不匹配。 Proper prefix suffix table for
"aaa"
would return 2. So only (3-2)=1 character move can be made according to KMP. "aaa"
正确前缀后缀表将返回2.因此,根据KMP,只能(3-2)= 1个字符移动。 So we are comparing n*(m-1)
times before we can conclude that there is no match in this case. 所以我们比较
n*(m-1)
次才能得出结论在这种情况下没有匹配。 Effectively time complexity is O(n*m)
. 有效的时间复杂度是
O(n*m)
。
Can somebody please explain how it is O(m+n)
in this case. 有人可以解释在这种情况下它是如何
O(m+n)
。 Do I have to consider anything else besides proper prefix suffix table and treat it as a special case. 我是否必须考虑除正确的前缀后缀表之外的任何其他内容并将其视为特殊情况。 Is it mentioned in KMP?
是在KMP中提到的吗? A detailed explanation for this particular scenario would be really helpful.
对这个特定场景的详细解释将非常有用。
So only (3-2)=1 character move can be made according to KMP.
因此,根据KMP,只能进行(3-2)= 1个字符移动。
Yes. 是。
So we are comparing n*(m-1) times before we can conclude that there is no match in this case.
所以我们比较n *(m-1)次才能得出结论在这种情况下没有匹配。
No. The next thing we compare is a
(4th of text) against a
(3rd of pattern), which gives 2+1=3 as the length of the current match. 号我们比较接下来的事情是
a
对(文本的第4次) a
(图案的第3次),这给2 + 1 = 3作为当前匹配的长度。
Let us work the T= aaaaaaaaa
text and P= aaab
pattern example. 让我们来处理T =
aaaaaaaaa
文本和P = aaab
模式示例。
As you can see, starting from position 4, for each position, we make two steps instead of one. 如您所见,从第4位开始,对于每个位置,我们分两步而不是一步。 But we are not comparing substrings at any moment, only individual characters.
但我们不是在任何时候比较子串,只是单个字符。 The number of positions is |T|, the number of negative steps is at most |T|, so the total number of steps is linear with respect to |T|.
位置数是| T |,负步数最多为| T |,因此总步数相对于| T |是线性的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.