码迷,mamicode.com
首页 > 其他好文 > 详细

kmp 模板

时间:2018-10-04 17:23:46      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:color   string   mes   queue   math.h   stream   iostream   mat   vector   

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<vector>
#include<queue>
using namespace std;
char x[10000];
char y[1000000];
int Next[10000];
void build_Next(){
    int lenx=strlen(x);
    int j=0,k=-1;
    Next[0]=-1;
    while(j<lenx){
        if(k==-1||x[j]==x[k]){
            j++;
            k++;
            Next[j]=k;
        }
        else{
            k=Next[k];
        }
    }
}
int zhao(){
    build_Next();
    int j=0,k=0,ans=0,lenx=strlen(x),leny=strlen(y);
    while(j<leny){
        if(k==-1||x[k]==y[j]){
            j++;
            k++;
        }
        else{
            k=Next[k];
        }
        if(j==leny){
            ans++;
            k=Next[k];
        }
    }
    return ans;
}
int main()
{
    scanf("%s",x);
    scanf("%s",y);
    printf("%d\n",zhao());
    return 0;
}

 

kmp 模板

标签:color   string   mes   queue   math.h   stream   iostream   mat   vector   

原文地址:https://www.cnblogs.com/fzw1523/p/9742513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!