字符串匹配 KMP算法 模板

KMP:Knuth(D.E.Knuth)、Morris(J.H.Morris)和Pratt(V.R.Pratt)三人设计的线性时间字符串匹配算法。KMP算法是字符串匹配的经典算法。KMP算法是通过分析子串,预先计算每个位置发生不匹配的时候,直接移动到下一个”恰当“的位置。其中的关键是计算jump数组。(相关证明可以看算法导论二版32.4)

时间复杂度为O(n);

/*kmp相关ojhttp://www.cnblogs.com/wuyiqi/archive/2012/01/06/2315188.html*/
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
const int T=1000009;
const int W=10009;
/*在jump数组中,发现str[k+1]!=str[i]时,当前的最长proper后缀不能完全匹配字符串的前缀 ,
当前的后缀需要缩短后再尝试。
要求缩短后的后缀也要是字符串的一个前缀时,就意味着在str[i-k+x..i]中(x<=k,其实就是str[i-k..i]的proper后缀)
寻找一个x使得str[i-k..i]的后缀str[i-k+x..i]为整个字符串的一个前缀。
由于str[i-k..i]与str[1..k]完全匹配,问题就转化成了求str[1...k]的最长proper后缀为字符串前缀,也就是jump[k]。*/
void getp(const char *str,int len,int *&jump){//string from the index of 1
    jump=new int[len+1];//is the proper suffix str' length for longest prefix
	jump[1]=0;//
	int k=0;
	for(int i=2;i<=len;i++){
		while(k>0&&str[k+1]!=str[i])
				k=jump[k];//if this suffix it not the prefix,decrease suffix size
		if(str[k+1]==str[i])
			k=k+1;
		jump[i]=k;    
	}
}
int kmp(const char *s,const char *t){//string from the index of 1
    if(s==NULL||t==NULL)
        return 0;
    int len_s=strlen(s+1);
    int len_t=strlen(t+1);
	int *jump,i,count=0;
	int j=0;//number of charachers matched
	    getp(s,len_s,jump);
	for(i=1;i<=len_t;i++){
		while(j>0&&s[1+j]!=t[i])
			j=jump[j];//next character does not match, decrease match number to jump[j]
		if(s[1+j]==t[i])
			j++;//nes character matcheds,matched number ++
		if(j==len_s){//is all of  pattern matched?
			count++;
			j=jump[j];//look for the next match
		}
	}
    return count;
}
int main(){
    int n;
    char w[W],t[T];
    scanf("%d",&n);
    while(n--){
        scanf("%s%s",w+1,t+1);
        int count=kmp(w,t);
        printf("%d\n",count);
    }
    return 0;
}

例题:

KMP 的直接使用:http://poj.org/problem?id=3461

参考:

  • 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、付费专栏及课程。

余额充值