串的匹配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
    评论
KMP算法是一种用于字符串匹配的算法,它的核心思想是利用部分匹配表来避免不必要的比较。下面是KMP算法的原理和实现步骤: 1. 部分匹配表的计算: - 部分匹配值是指字符串的前缀和后缀的最长公共部分的长度。 - 部分匹配表是一个数组,记录了每个位置的部分匹配值。 - 部分匹配表的计算可以通过动态规划的方式进行,具体步骤如下: - 初始化部分匹配表的第一个元素为0。 - 从第二个元素开始,依次计算每个位置的部分匹配值: - 如果当前位置的符与前一个位置的部分匹配值对应的符相等,则部分匹配值加1。 - 如果不相等,则需要回溯到前一个位置的部分匹配值对应的符的部分匹配值,继续比较。 - 在主中从左到右依次比较符,同时在模式中根据部分匹配表进行跳跃。 - 如果当前匹配成功,则继续比较下一个符。 - 如果当前匹配失败,则根据部分匹配表找到模式中需要跳跃的位置,继续比较。 下面是一个使用KMP算法进行字符串匹配的示例代码: ```python def kmp_search(text, pattern): n = len(text) m = len(pattern) next = get_next(pattern) i = 0 j = 0 while i < n and j < m: if j == -1 or text[i] == pattern[j]: i += 1 j += 1 else: j = next[j] if j == m: return i - j else: return -1 def get_next(pattern): m = len(pattern) next = [-1] * m i = 0 j = -1 while i < m - 1: if j == -1 or pattern[i] == pattern[j]: i += 1 j += 1 next[i] = j else: j = next[j] return next ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值