Code
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int N=1000005;
int n,m,p[N];
char s1[N],s2[N];
void pre_deal()
{
p[1]=0;
int j=0;
for(int i=1;i<m;i++)
{
while(j>0&&s2[i+1]!=s2[j+1]) j=p[j];
if(s2[i+1]==s2[j+1]) j++;
p[i+1]=j;
}
return;
}
void kmp()
{
int j=0;
for(int i=0;i<n;i++)
{
while(j>0&&s1[i+1]!=s2[j+1]) j=p[j];
if(s1[i+1]==s2[j+1]) j++;
if(j==m)
{
printf("%d
",i+1-m+1);
j=p[j];
}
}
return;
}
int main()
{
cin>>(s1+1)>>(s2+1);
n=strlen(s1+1);
m=strlen(s2+1);
pre_deal();
kmp();
for(int i=1;i<=m;i++) printf("%d ",p[i]);
printf("
");
return 0;
}
Classical is something not fade,but grow more precious with time pass by,so is dream.梦想这东西和经典一样,永远不会因为时间而褪色,反而更显珍贵。