简体   繁体   English

KMP模式匹配算法的时间复杂度可以是O(m * n)?

[英]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模式示例。

  • On position 1, we have T[1]=P[1], so the length of the match is 1. 在位置1,我们有T [1] = P [1],所以匹配的长度是1。
  • On position 2, we have T[2]=P[2], so the length of the match is 2. 在位置2,我们有T [2] = P [2],所以匹配的长度是2。
  • On position 3, we have T[3]=P[3], so the length of the match is 3. 在位置3,我们有T [3] = P [3],所以匹配的长度是3。
  • On position 4, we have T[4]≠P[4]. 在位置4,我们有T [4]≠P [4]。 The length of the match reduces to pref(3)=2 ( negative step ). 匹配的长度减少到pref(3)= 2( 负步 )。
  • On position 4 again, we have T[4]=P[3], so the length of the match is 3. 再次在第4位,我们有T [4] = P [3],所以匹配的长度是3。
  • On position 5, we have T[5]≠P[4]. 在位置5,我们有T [5]≠P [4]。 The length of the match reduces to pref(3)=2 ( negative step ). 匹配的长度减少到pref(3)= 2( 负步 )。
  • On position 5 again, we have T[5]=P[3], so the length of the match is 3. 再次在位置5,我们有T [5] = P [3],所以匹配的长度是3。
  • And so on. 等等。

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.

相关问题 KMP算法的时间复杂度 - Time complexity of KMP algorithm 该算法的时间复杂度:是O(n ^ 2)还是O(n) - Time Complexity of this Algorithm: is it O(n^2) or O(n) 该函数的时间复杂度是O(N)还是O(N ^ 2)? - Is the time complexity for this function O(N) or O(N^2)? 使用Ukkonen算法的时间复杂度,Knuth–Morris–Pratt(KMP)和后缀树之间的差异。 - Difference between Knuth–Morris–Pratt (KMP) and suffix tree using Ukkonen's algorithm for time complexity. 反转字符串:这是 O(log n) 时间复杂度吗? - Reversing a String: Is this O(log n) time complexity? 在O(m + n)时间中从字符串中的特定字母查找所有子字符串的算法 - algorithm for finding all substrings from a specific alphabet in a string in O(m+n) time 尝试获取 KMP 算法中模式的前缀表 - Trying to get prefix table for the pattern in KMP algorithm 字符串切片的时间复杂度是多少? O(k) 或 O(n) - What is the time complexity of string slice? O(k) or O(n) String.toCharArray(),O(n)或O(1)的时间复杂度是多少 - What is the time complexity of String.toCharArray(), O(n) or O(1) 编写反向字符串O(N)时间复杂度和O(N)空间复杂度的程序 - Write program for Reverse string O(N) time complexity and O(N) space complexity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM