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

luogu_3375 【模板】KMP字符串匹配

时间:2017-07-01 10:04:44      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:字符串匹配   its   str   using   name   namespace   ack   print   hit   

#include<bits/stdc++.h>

using namespace std;

int f[1000010],n,m;

char p[1000010],t[1000010];

void getfail(){

f[0]=f[1]=0;

for(int i=1;i<m;i++){

int j=f[i];

while(j && p[i]!=p[j])j=f[j];

f[i+1]=p[i]==p[j]?j+1:0;

}

}

void find(){

getfail();

int j=0;

for(int i=0;i<n;i++){

while(j && t[i]!=p[j])j=f[j];

if(t[i]==p[j])j++;

if(j==m)printf("%d\n",i-m+2);

}

}

int main(){

scanf("%s%s",&t,&p);

n=strlen(t); m=strlen(p);

find();

for(int i=1;i<=m;i++)printf("%d ",f[i]);

puts("");

return0;

}

luogu_3375 【模板】KMP字符串匹配

标签:字符串匹配   its   str   using   name   namespace   ack   print   hit   

原文地址:http://www.cnblogs.com/codetogether/p/7101233.html

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