点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int kmp[maxn];
int la,lb,j;
char a[maxn],b[maxn];
int main(){
cin>>a+1>>b+1;
la=strlen(a+1),lb=strlen(b+1);
for(int i=2;i<=lb;i++){
while(j&&b[j+1]!=b[i]){
j=kmp[j];
}
if(b[j+1]==b[i] ){
j+=1;
}
kmp[i]=j;
}
j=0;
for(int i=1;i<=la;i++){
while(j&&b[j+1]!=a[i]){
j=kmp[j];
}
if(b[j+1]==a[i]) j+=1;
if(j==lb){
cout<<i-j+1<<endl;
j=kmp[j];
}
}
for(int i=1;i<=lb;i++) cout<<kmp[i]<<" ";
return 0;
}