# 【bzoj4259】残缺的字符串 FFT

3 7
ab
aebr
ob

2
1 5

## Sol

$$\sum(a_i-b_i)^2=\sum a_i^2*[b_i>0]+\sum b_i^2*[a_i>0]-2*\sum a_ib_i$$

## Code

#include <bits/stdc++.h>
#define pi acos(-1)
using namespace std;
int i,j,k,n,m,len,a[1048577],b[1048577],ans[1048577],tot;char aa[1048577],bb[1048577];
struct cp
{
double x,y;
cp(double x=0.0,double y=0.0):x(x),y(y){}
cp operator +(const cp a)const{return cp(x+a.x,y+a.y);}
cp operator -(const cp a)const{return cp(x-a.x,y-a.y);}
cp operator *(const cp a)const{return cp(x*a.x-y*a.y,x*a.y+y*a.x);}
}w,wn,t,A[1048577],B[1048577],C[1048577];
void fft(cp *a,int n,int op)
{
for(i=k=0;i<n;i++){if(i>k) swap(a[i],a[k]);for(j=(n>>1);(k^=j)<j;j>>=1);}
for(k=2,wn=cp(cos(2*pi*op/k),sin(2*pi*op/k));k<=n;k<<=1,wn=cp(cos(2*pi*op/k),sin(2*pi*op/k)))
for(i=0,w=cp(1,0);i<n;i+=k,w=cp(1,0)) for(j=0;j<(k>>1);j++,w=w*wn)
t=w*a[i+j+(k>>1)],a[i+j+(k>>1)]=a[i+j]-t,a[i+j]=a[i+j]+t;
if(op==-1) for(int i=0;i<n;i++) a[i].x/=n;
}
int main()
{
for(scanf("%d%d%s%s",&m,&n,aa,bb),i=0,j=m-1;i<j;i++,j--) swap(aa[i],aa[j]);
for(i=0;i<m;i++) if(aa[i]!='*') a[i]=aa[i]-'a'+1;
for(i=0;i<n;i++) if(bb[i]!='*') b[i]=bb[i]-'a'+1;
for(len=1;len<n+m;len<<=1);
for(i=0;i<len;i++) A[i]=cp(a[i]*a[i],0),B[i]=cp(b[i]>0,0);
for(fft(A,len,1),fft(B,len,1),i=0;i<len;i++) C[i]=C[i]+A[i]*B[i];
for(i=0;i<len;i++) A[i]=cp(a[i]>0,0),B[i]=cp(b[i]*b[i],0);
for(fft(A,len,1),fft(B,len,1),i=0;i<len;i++) C[i]=C[i]+A[i]*B[i];
for(i=0;i<len;i++) A[i]=cp(a[i],0),B[i]=cp(b[i],0);
for(fft(A,len,1),fft(B,len,1),i=0;i<len;i++) C[i]=C[i]-A[i]*B[i]*cp(2.0,0.0);
for(fft(C,len,-1),i=m-1;i<n;i++) if(C[i].x<0.1) ans[++tot]=i-m+2;
for(printf("%d\n%d",tot,ans[1]),i=2;i<=tot;i++) printf(" %d",ans[i]);
}

posted @ 2018-08-02 15:56  CK6100LGEV2  阅读(156)  评论(0编辑  收藏  举报