串的匹配KMP

1、详解

http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html

2、主字符串的子字符串位置

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STRING_INIT_SIZE    100    //串初始长度  


int Index(char *S, char *T, int pos){
    //返回字串T在主串S中第pos个字符之后的位置。若不存在,则函数值为0.
    //其中,T非空,1<=pos<=StrLength(s).
    int i = pos;
    int j = 1;
    while (i <= S[0] && j <= T[0]){
        if (S[i++] != T[j++]){
            i = i - j + 2;
            j = 1;
        }else{
			++i;++j;
		}
    }
    return (j > T[0]) ? i - T[0] : -1;
}

int get_next(char *T, int next[]){
    //求模式串T的next函数值并存入数组next。
    int i = 1, j = 0;
    next[1] = 0;
    while (i<T[0]){
        if (j == 0 || T[i] == T[j])
            next[++i] = ++j;
        else
            j = next[j];
    }
    return *next;
}
int Index_KMP(char *S, char *T, int pos){
    //利用模式串T的next函数求T在主串S中第pos个字符之后的位置的KMP算法,其中T非空,1<=pos<=StrLength(S).
    int next[100];
    *next = get_next(T, next);
    int j = 1, i = pos;
    while (i <= S[0] && j <= T[0]){
        if (j == 0 || S[i] == T[j])
            { ++i; ++j; }
        else 
            j = next[j];
    }
    return (j > T[0]) ? i - T[0] : -1;
}
int main(){
    char S[STRING_INIT_SIZE],T[STRING_INIT_SIZE];
    int s;
    //数组内数据存储格式:
    //{[元素0:串的长度],[元素1:第1个字符],[元素2:第2个字符],[元素3:第3个字符]……}
    printf("请输入主串:");
    gets(S + 1);            //从[1]单元开始存放串
    *S = strlen(S + 1);     //将串的长度保存至[0]单元
    printf("请输入子串:");
    gets(T + 1);
    *T = strlen(T + 1);
    printf("开始匹配的位置:");
    scanf("%d",&s);

    s = Index(S, T, s);     //使用普通方式匹配
    printf("子串的位置(普通):%d\n",s);
    s = Index_KMP(S, T, s); //使用改进方式匹配
    printf("子串的位置(改进):%d\n",s);

    system("pause");
}

更加优化的方法:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING_INIT_SIZE    100    //串初始长度  
int get_nextval(char *T, int nextval[]){
    //求模式串T的nextval函数值并存入数组nextval。
    int i = 1, j = 0;
    nextval[1] = 0;
    while (i<T[0]){
        if (j == 0 || T[i] == T[j]){
        	++i;++j;
           if(T[i]!=T[j]) nextval[i]=j;
           else nextval[i]=nextval[j];
		}
        else{
        	j=nextval[j];
		}        
    }
    return *nextval;
}
int Index_KMP(char *S, char *T, int pos){
    //利用模式串T的next函数求T在主串S中第pos个字符之后的位置的KMP算法,其中T非空,1<=pos<=StrLength(S).
    int nextval[100];
    *nextval = get_nextval(T, nextval);
    int j = 1, i = pos;
    while (i <= S[0] && j <= T[0]){
        if (j == 0 || S[i] == T[j])
            { ++i; ++j; }
        else 
            j = nextval[j];
    }
    return (j > T[0]) ? i - T[0] : -1;
}
int main(){
    char S[STRING_INIT_SIZE],T[STRING_INIT_SIZE];
    int s;
    //数组内数据存储格式:
    //{[元素0:串的长度],[元素1:第1个字符],[元素2:第2个字符],[元素3:第3个字符]……}
    printf("请输入主串:");
    gets(S + 1);            //从[1]单元开始存放串
    *S = strlen(S + 1);     //将串的长度保存至[0]单元
    printf("请输入子串:");
    gets(T + 1);
    *T = strlen(T + 1);
    printf("开始匹配的位置:");
    scanf("%d",&s);

    s = Index_KMP(S, T, s); 
    printf("子串的位置(改进):%d\n",s);

    system("pause");
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值