1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| #include<iostream> #include<cstdio> #include<complex> #include<cmath> #include<algorithm> using namespace std; const int M=300000+100; const int N=M*4; const double PI=acos(-1); const double eps=1e-1; typedef complex <double> cp; cp omega(int K,int n) { return cp(cos(2*PI*K/n),sin(2*PI*K/n)); } inline void FFT(cp a[],int n,bool type) { static int tmp[N],num=n-1,len; while(num!=0) num/=2,len++; for(int i=0,j;i<=n;i++) { for(j=0,num=i;j<len;j++) tmp[j]=num%2,num/=2; reverse(tmp,tmp+len); for(j=0,num=0;j<len;j++) num+=tmp[j]*(1<<j); if(i<num) swap(a[i],a[num]); } for(int l=2;l<=n;l*=2) { int m=l/2; cp x0=omega(1,l); if(type==true) x0=conj(x0); for(int i=0;i<n;i+=l) { cp x(1,0); for(int j=0;j<m;j++,x*=x0) { cp temp=x*a[i+j+m]; a[i+j+m]=a[i+j]-temp; a[i+j]=a[i+j]+temp; } } } } char A[N],B[N]; int m,n,t=1; cp S1[N],S2[N],S3[N],B1[N],B2[N],B3[N]; bool OK[N]; int main() { scanf("%d%d%s%s",&m,&n,A,B);
while(t<=(n+m)) t*=2; reverse(A,A+m); for(int i=0;i<t;i++) A[i]=(A[i]=='*' or A[i]<'a' or A[i]>'z'?0:A[i]-'a'+1), B[i]=(B[i]=='*' or B[i]<'a' or B[i]>'z'?0:B[i]-'a'+1); for(int i=0;i<t;i++) S1[i]=A[i],S2[i]=A[i]*A[i],S3[i]=A[i]*A[i]*A[i]; for(int i=0;i<t;i++) B1[i]=B[i],B2[i]=B[i]*B[i],B3[i]=B[i]*B[i]*B[i]; FFT(S1,t,false); FFT(S2,t,false); FFT(S3,t,false); FFT(B1,t,false); FFT(B2,t,false); FFT(B3,t,false);
for(int i=0;i<t;i++) S3[i]*=B1[i]; for(int i=0;i<t;i++) S1[i]*=B3[i]; for(int i=0;i<t;i++) S2[i]*=B2[i]; for(int i=0;i<t;i++) S3[i]+=S1[i]-2.0*S2[i]; FFT(S3,t,true); int cnt=0; for(int i=m-1;i<n;i++) if(fabs(S3[i].real()/t)<eps) OK[i]=true,cnt++;
printf("%d\n",cnt); for(int i=m-1;i<n;i++) if(OK[i]==true) printf("%d ",i-m+1+1); return 0; }
|