繁体   English   中英

KMP字符串匹配算法:辅助数组输出

[英]KMP string matching algorithm:Auxillary array output

这是我对KMP字符串匹配算法的实现 当我检查pi数组时,它存储0、1、2、3、4、5、6。 但是根据算法书籍,它应该是0,0,1,2,3,0,1。 我的代码也给出正确的结果。我不明白为什么会这样,还是我做错了什么? 如果是这样,请纠正我。

谢谢。

#include<iostream>
#include<string>
#include<string.h>

using namespace std;

int* ComputePrefix(char P[])
{
    size_t m = strlen(P);
    int *pi = new int[m];
    pi[0] = 0;
    int k = 0;

    for(int q =0; q < m; q++)
    {
        if( k > 0 && P[k+1] != P[q])
            k = pi[k];

        if( P[k+1] == P[q])
            {
                pi[q] = k;
                k = k + 1;
            }
            pi[q]=k;
    }

    return (pi);
}

void KMP_Matcher(char T[], char P[])
{

    size_t n = strlen(T);
    size_t m = strlen(P);

    int *pi = new int[m];
    pi = ComputePrefix(P);

    cout<<endl;


    int q =0;
    for (int i = 0; i <= n; i++)
    {
        if( q > 0 && P[q] != T[i] )
        {
            q = pi[q - 1];
        }


        else if( P[q] == T[i])
        {


            if( q == m-1)
            {
                cout<<"Shift occurs at : "<< i-q <<endl;
                q = pi[q];
            }
            else q = q + 1;
        }

        else q++;
    }
}


int main()
{
    char T[] = "abababacaba";
    char P[] = "ababaca";

    KMP_Matcher(T,P);
    return 0;
}

您的跳转表构造函数根本不会检查指针的前缀。 我们希望能够针对针中的每个位置查找导致(但不包括)该位置的针的尽可能长的适当前缀的长度,而不是从needle[0]开始的完整前缀只是不匹配; 这是寻找下一场比赛我们必须回溯的距离。 因此,跳转表中的每个条目(例如, table[i] )恰好是针的最长可能适当前缀的长度,该长度也是以needle[i - 1]结尾的子串的前缀。

跳转表中的前两个条目分别为-1和0,因为a)模式开头的不匹配不会触发回溯(换句话说,长度为零的前缀不能有任何适当的前缀或后缀),并且b)空字符串被认为长度为0。

有关更多详细信息,请参阅Wikipedia或算法教科书。

完成以上操作的代码是:

int *build_jump_table(const char * target)
{
    if(!target)
        return NULL;
    int *table = new int[strlen(target) + 1];
    if(!table)
        return NULL;
    table[0] = -1; /* unused by the matcher, just used here */

    for(int i = 0; target[i] != '\0'; i++) {
        table[i+1] = table[i] + 1;
        while(table[i+1] > 0 && target[i] != target[table[i+1] - 1]) {
            table[i + 1] = table[table[i + 1] - 1] + 1;
        }
    }
    return table;
}

这非常冗长,当您了解跳转表背后的概念时,可以进行很多简化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM