直接上代码
import java.io.*;
public class Main {
static int N=100010;
static int M=1000010;
static int n=0;
static int m=0;
static char []p=new char[N];
static char []s=new char[M];
static int ne[]=new int[N];
public static void main (String[] args) throws IOException {
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
BufferedWriter bw=new BufferedWriter (new OutputStreamWriter (System.out));
n=Integer.parseInt (br.readLine ());
char cs[]=br.readLine ().toCharArray ();
System.arraycopy (cs,0,p,1,n);
m=Integer.parseInt (br.readLine ());
cs=br.readLine ().toCharArray ();
System.arraycopy (cs,0,s,1,m);
for (int i = 2, j=0; i <=n ; i++) {
while (j!=0 && p[i]!=p[j+1]){
j=ne[j];
}
if(p[i]==p[j+1]){
j++;
}
ne[i]=j;
}
for(int i=1,j=0;i<=m;i++){
while (j!=0 &&s[i]!=p[j+1]){
j=ne[j];
}
if(s[i]==p[j+1]){
j++;
}
if(j==n){
bw.write (i-n+" ");
j=ne[j];
}
}
bw.flush ();
}
}