首页 > 其他 > 详细

KMP模板

时间:2016-07-24 14:39:43      阅读:180      评论:0      收藏:0      [点我收藏+]
#include<stdio.h>
#include<string.h>
const int N=1e6+11;
int next[N];
void getnext(char *s){
    next[0]=0; 
    for(int i=1,j=0;s[i];i++){//j为next[i-1] 
        while(j>0&&s[i]!=s[j]){
            j=next[j-1];
        }
        if(s[i]==s[j]) j++;
        next[i]=j;
    }
} 
int KMP(char *O,char *F){
    int n=strlen(O),m=strlen(F);
    int i,j,ans=0;
    getnext(F);
    for(i=0,j=0;i<n;i++){
        while(j>0&&O[i]!=F[j]){
            j=next[j-1];
        }
        if(O[i]==F[j])    ++j;
        if(j==m)    ans++;
    }
    return ans;
} 

 

KMP模板

原文:http://www.cnblogs.com/L-King/p/5700680.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!