KMP 扩展KMP

  1. kmp解决了求所有主串的前缀pre[i] (0 <= i < n)的后缀与模式串前缀的最大匹配长度问题
  2. 拓展kmp解决了所有主串的后缀suf[i]的前缀,与模式串前缀的最大匹配长度问题.
  3. 当且仅当len%(len-next[len])==0时,str[next[len]~len-1]为最小循环节
//KMP
int kmp(char str[],char sub[]){
    int i,j,counts = 0;
    int lenstr = strlen(str);
    int lensub = strlen(sub);

    int next[lensub+1];
    memset(next,0,sizeof(next));
    for(i = 1; i < lensub; i++){
        j = next[i-1];
        while(sub[i] != sub[j] && j > 0)
            j = next[j-1];
        if(sub[i] == sub[j]){
            next[i] = j + 1;
            if(j > 0)//匹配时的优化,单纯求next数组没有这条语句
                next[i-1] = next[j-1];
        }
    }

    i = 0;  j = 0;
    while(i < lenstr){
        if(str[i] == sub[j]){
            i++;  j++;
        }
        else{
            if(j == 0)
                i++;
            else
                j = next[j-1];
        }
        if(j == lensub){
            counts++;  j = next[j-1];
        }
    }
    return counts;
}
//扩展KMP
const int MAXN = 200010;
int nexts[MAXN], extend[MAXN];
void getNext(char T[]){
    int length = strlen(T);
    int i,a,p, j = -1;
    nexts[0] = length;
    for(i = 1; i < length; ++i,--j){
        if(j<0 || i+nexts[i-a] >= p){
            if(j < 0)
                j = 0, p = i;
            while(p<length && T[p]==T[j])
                ++j, ++p;
            nexts[i] = j, a = i;
        }
        else nexts[i] = nexts[i-a];
     }
}
void getExtend(char S[], char T[]){
    memset(nexts,0,sizeof(nexts));
    getNext(T);
    int Slen = strlen(S), Tlen = strlen(T);
    int i,a,p, j = -1;

    for(i = 0; i < Slen; ++i,--j){
        if(j<0 || i+nexts[i-a] >= p ){
            if(j < 0)
                j = 0, p = i;
            while(p<Slen && j<Tlen && S[p]==T[j])
                ++j, ++p;
            extend[i] = j;  a = i;
        }
        else extend[i] = nexts[i-a];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值