标签:main length http ace cin kmp ++ cout n+1
#include<bits/stdc++.h>
using namespace std;
string a,b;
int nest[1000005];
int m,n;
void q_next() {
int k=0;
for(int i=2; i<=n; i++) {
while(k>0&&b[i]!=b[k+1])
k=nest[k];
if(b[i]==b[k+1])
k++;
nest[i]=k;
}
}
int main() {
cin>>a;
cin>>b;
m=a.length();
n=b.length();
a=' '+a;
b=' '+b;
q_next();
int k=1;
for(int i=1; i<=m; i++) {
while(k>0&&a[i]!=b[k+1])
k=nest[k];
if(a[i]==b[k+1])
k++;
if(k==n) {
cout<<i-n+1<<"\n";
k=nest[k];
}
}
for(int i=1; i<=n; i++)
cout<<nest[i]<<" ";
return 0;
}
标签:main length http ace cin kmp ++ cout n+1
原文地址:https://www.cnblogs.com/mxy2004/p/12011771.html