c++ - KMP算法和LPS表构建的运行时间

标签 c++ runtime string-matching knuth-morris-pratt longest-prefix

我最近遇到了 KMP 算法,我花了很多时间试图理解它为什么起作用。虽然我现在确实了解基本功能,但我只是不了解运行时计算。

我从 geeksForGeeks 网站获取了以下代码:https://www.geeksforgeeks.org/kmp-algorithm-for-pattern-searching/

该站点声称,如果文本大小为 O(n) 且模式大小为 O(m),则 KMP 会在最大 O(n) 时间内计算匹配项。它还指出可以在 O(m) 时间内计算 LPS 数组。

// C++ program for implementation of KMP pattern searching 
// algorithm 
#include <bits/stdc++.h> 

void computeLPSArray(char* pat, int M, int* lps); 

// Prints occurrences of txt[] in pat[] 
void KMPSearch(char* pat, char* txt) 
{ 
    int M = strlen(pat); 
    int N = strlen(txt); 

    // create lps[] that will hold the longest prefix suffix 
    // values for pattern 
    int lps[M]; 

    // Preprocess the pattern (calculate lps[] array) 
    computeLPSArray(pat, M, lps); 

    int i = 0; // index for txt[] 
    int j = 0; // index for pat[] 
    while (i < N) { 
        if (pat[j] == txt[i]) { 
            j++; 
            i++; 
        } 

        if (j == M) { 
            printf("Found pattern at index %d ", i - j); 
            j = lps[j - 1]; 
        } 

        // mismatch after j matches 
        else if (i < N && pat[j] != txt[i]) { 
            // Do not match lps[0..lps[j-1]] characters, 
            // they will match anyway 
            if (j != 0) 
                j = lps[j - 1]; 
            else
                i = i + 1; 
        } 
    } 
}

// Fills lps[] for given patttern pat[0..M-1] 
void computeLPSArray(char* pat, int M, int* lps) 
{ 
    // length of the previous longest prefix suffix 
    int len = 0; 

    lps[0] = 0; // lps[0] is always 0 

    // the loop calculates lps[i] for i = 1 to M-1 
    int i = 1; 
    while (i < M) { 
        if (pat[i] == pat[len]) { 
            len++; 
            lps[i] = len; 
            i++; 
        } 
        else // (pat[i] != pat[len]) 
        { 
            // This is tricky. Consider the example. 
            // AAACAAAA and i = 7. The idea is similar 
            // to search step. 
            if (len != 0) { 
                len = lps[len - 1]; 

                // Also, note that we do not increment 
                // i here 
            } 
            else // if (len == 0) 
            { 
                lps[i] = 0; 
                i++; 
            } 
        } 
    } 
} 

// Driver program to test above function 
int main() 
{ 
    char txt[] = "ABABDABACDABABCABAB"; 
    char pat[] = "ABABCABAB"; 
    KMPSearch(pat, txt); 
    return 0; 
}

我真的很困惑为什么会这样。

对于 LPS 计算,请考虑:aaaaacaaac
在这种情况下,当我们尝试为第一个 c 计算 LPS 时,我们将继续返回,直到我们达到 LPS[0],即 0 并停止。因此,基本上,我们将至少返回模式的长度,直到该点。如果这种情况发生多次,时间复杂度如何为 O(m)?

我对 KMP 的运行时 O(n) 有类似的困惑。

在发布之前,我已经阅读了堆栈溢出中的其他线程,以及有关该主题的各种其他站点。我仍然很困惑。如果有人能帮助我理解这些算法的最佳和最坏情况,以及如何使用一些示例计算它们的运行时间,我将不胜感激。再次,请不要建议我用谷歌搜索这个,我已经完成了,花了整整一周的时间试图获得任何见解,但失败了。

最佳答案

为构建 LPS 数组建立运行时上限的一种方法是考虑一种病态情况 - 我们如何最大化必须执行 len = lps[len - 1] 的次数?考虑以下字符串,忽略空格:x1 x2 x1x3 x1x2x1x4 x1x2x1x3x1x2x1x5 ...
第二项需要与第一项进行比较,就好像它以 1 而不是 2 结尾一样,它将匹配第一项。类似地,第三项需要与前两项进行比较,就好像它以 1 或 2 而不是 3 结尾一样,它将匹配那些部分项。等等。
在示例字符串中,很明显只有每 1/2^n 个字符可以匹配 n 次,因此总运行时间将为 m+m/2+m/4+..=2m=O(m),长度模式字符串。我怀疑构建一个运行时间比示例字符串更糟糕的字符串是不可能的,这可能会被正式证明。

关于c++ - KMP算法和LPS表构建的运行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53072431/

相关文章:

c++ - 我的 boost phoenix 惰性函数出了什么问题?

c++ - 如何加速 R 代码在特定方向上移动矩阵元素 - Rccp?

python - 为什么我的随机主元快速排序比固定主元快速排序慢?

linux - 使用grep匹配整个单词的问题

perl 读取文件并抓取特定行

javascript - 在 jQuery 中匹配标签和字符串

c++ - 我收到一个错误 [错误 : expected unqualified-id before ‘&’ token ] in a c++ program

python - 如何禁用 Django 内部函数缓存?

iOS - 将框架 Storyboard链接到 ViewController 以便在主项目中使用

c++ - 如何避免使用 Try Catch (C++) 在整数值中输入字符串