@[tiandragon00](/space/show?uid=33639)
帮你改了一下。
```cpp
#include<bits/stdc++.h>
#define next nxt
using namespace std;
int next[1000005];
char t[1000005],p[1000005];
int x,y;
void KMP(char t[],char p[])
{
int flag=0;
int i = 0;
int j = 0;
int x=strlen(t); //cout<<"asdf"<<x<<endl;
int y=strlen(p);
//cout<<x<<" "<<y<<endl;
while (i < x && j <y)
{
if (j == -1 || t[i] == p[j])
{
//printf("i=%d,j=%d\n",i,j);
i++;
j++;
}
else
j = next[j];
if(j==y)
{
printf("%d\n",i-j+1);
flag=1;
j=next[j];
}
}
}
void getNext(char p[],int next[])
{
next[0] = -1;
int i = 0, j = -1;
int len=strlen(p);
while (i <= len)
{
if (j == -1 || p[i] == p[j])
{
++i;
++j;
next[i] = j;
}
else
j = next[j];
}
}
int main()
{
scanf("%s%s",t,p);
//cout<<t<<endl<<p<<endl;
getNext(p,next);
KMP(t, p);
//cout<<x+1<<endl;
x=strlen(t); //cout<<"asdf"<<x<<endl;
y=strlen(p);
for(int i=1;i<=y;i++)
printf("%d%c",next[i],i==y?'\n':' ');
return 0;
}
```
by Smile_Cindy @ 2019-03-19 19:32:52