kmp算法的c/c++实现

又看了一下kmp算法,资料是阮一峰的博客和b站一个博主(正月点灯笼)的视频。

下面代码注释的部分是正月点灯笼视频里面的写法,自己感觉有点太麻烦,改了一种写法,只是写法变了而已。具体原理请参见相关文章和视频。

代码如下:

# include 
# include 

using namespace std;
/*
 * 第一版本
void prefix_table(string pattern, int prefix[], int n)
{
    prefix[0] = 0;
    int len = 0;        // 最大前后缀匹配长度
    int i = 1;
    while (i < n)
    {
        if (pattern[i] == pattern[len]) //如果比前面长的话
        {
            len ++;
            prefix[i] = len;
            i ++;
        }
        else
        {
            if (len > 0)
                len = prefix[len - 1];          // 其实也相当于 O(n^2)了
            else
            {
                prefix[i] = len;    // = 0
                i ++;
            }
        }
    }
}*/

void prefix_table(string pattern, int prefix[], int n)
{
    prefix[0] = 0;
    int len = 0;        // 最大前后缀匹配长度
    int i = 1;
    for (; i < n ; i ++)
    {
        while (pattern[i] != pattern[len] && len > 0)
            len = prefix[len-1];
        
        if (pattern[i] == pattern[len]) //如果比前面长的话
            len ++;
        prefix[i] = len;
    }
}

void move_prefix_table(int prefix[], int n)
{
    for (int i = n-1; i > 0; i --)
    {
        prefix[i] = prefix[i-1];
    }
    prefix[0] = -1;
}
/*
 * 第一版
void kmp_search(const string text,const string pattern)
{
    int n = pattern.length();
    int m = text.length();
    int * prefix  = new int[n];
    prefix_table(pattern, prefix, n);
    move_prefix_table(prefix, n);
    
    int i = 0, j = 0;
    while (i < m)
    {
        if (j == n-1 && text[i] == pattern[j])
            cout << "Found pattern at " << i-j << endl;
        if (text[i] == pattern[j])
        {
            i ++; 
            j ++;
        }
        else
        {
            j = prefix[j];              // 相当于O(n^2)
            if (j == -1)
            {
                i ++; 
                j ++;
            }
        }
    }
}
*/
void kmp_search(const string text,const string pattern)
{
    int n = pattern.length();
    int m = text.length();
    int * prefix  = new int[n];
    prefix_table(pattern, prefix, n);
    move_prefix_table(prefix, n);
    int i = 0, j = 0;

    for (; i < m; i ++, j ++)
    {
        if (j == n-1 && text[i] == pattern[j])
            cout << "Found pattern at " << i-j << endl;

        while (text[i] != pattern[j] && j != -1)
        {
            j = prefix[j]; 
        }
    }
}

int main ()
{
    /*
    char pattern[] = "ABABCABAA";
    int prefix[9];
    int n = 9;

    prefix_table(pattern, prefix, n);
    move_prefix_table(prefix, n);
    for (int i = 0; i < n; i ++)
        cout << prefix[i] << " ";
    cout << endl;*/

    string text = "ababacababababababbaabbababaabaababacabababababbcababbabababcababbaababacab";
    string pattern = "ababacab";
    kmp_search(text, pattern);
    return 0;
}

输出结果:
Found pattern at 0
Found pattern at 31
Found pattern at 67

参考资料:
阮一峰博客
b站:正月点灯笼

你可能感兴趣的:(算法)