KMP模板

来源:互联网 发布:数太奇大数据研究中心 编辑:程序博客网 时间:2024/05/04 14:56

KMP是字符串处理最基本方式。并且非常有效。

#include<stdio.h>#include<string.h>#include<iostream>using namespace std;char c[100005];int nex[100005];int main(){    int i,j,m;    cin>>c;    m=strlen(c);    i=0;j=-1;    nex[0]=-1;    while(i<m)    {        if(j==-1||c[i]==c[j])        {            i++;            j++;            nex[i]=j;        }        else        {            j=nex[j];        }    }    return 0;}


0 0