# BZOJ4259 残缺的字符串

$\sum f[i]^3 g[j] -2f[i]^2g[j]^2 +f[i]g[j]^3$

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define inf 20021225
#define ll long long
#define db double
#define mxn 1200010
#define eps 1e-8
using namespace std;

const db pi = acos(-1.0);

struct cpx
{
db x,y;
cpx(){}
cpx(db _x,db _y){x=_x,y=_y;}
};

cpx operator +(cpx a,cpx b){return cpx(a.x+b.x,a.y+b.y);}
cpx operator -(cpx a,cpx b){return cpx(a.x-b.x,a.y-b.y);}
cpx operator *(cpx a,cpx b){return cpx(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
int rev[mxn];
int init(int n)
{
int lim=1,l=0;
while(lim<n) lim<<=1,l++;
for(int i=1;i<lim;i++)
rev[i]=(rev[i>>1]>>1)|((i&1)<<(l-1));
return lim;
}

void fft(cpx *a,int n,int f)
{
for(int i=1;i<n;i++)	if(rev[i]>i)
swap(a[rev[i]],a[i]);
for(int k=2,mid=1;k<=n;k<<=1,mid<<=1)
{
cpx Wn = cpx(cos(pi/mid),f*sin(pi/mid));
for(int i=0;i<n;i+=k)
{
cpx w = cpx(1.0,0.0);
for(int j=0;j<mid;j++,w=w*Wn)
{
cpx x=a[i+j],y=w*a[i+mid+j];
a[i+j] = x+y; a[i+mid+j] = x-y;
}
}
}
if(f==-1)	for(int i=0;i<n;i++)	a[i].x/=n;
}
char s[mxn],t[mxn]; int ls,lt,ans;
cpx f1[mxn],f2[mxn],f3[mxn],g1[mxn],g2[mxn],g3[mxn],h[mxn];
vector<int> vec;
int id(char ch){return ch=='*'?0:ch-'a'+1;}
int main()
{
//freopen("1.in","r",stdin);
scanf("%d%d",&lt,&ls);
scanf("%s%s",t,s);
for(int i=0;i<ls;i++)
f1[i].x=(db)id(s[i]),
f2[i].x=f1[i].x*f1[i].x,
f3[i].x=f1[i].x*f2[i].x;
for(int i=0;i<lt;i++)
g1[i].x=(db)id(t[lt-i-1]),
g2[i].x=g1[i].x*g1[i].x,
g3[i].x=g2[i].x*g1[i].x;
//for(int i=0;i<ls;i++)	printf("%lf ",f[i].x);
int lim=init(ls+lt);
fft(f1,lim,1); fft(f2,lim,1); fft(f3,lim,1);
fft(g1,lim,1); fft(g2,lim,1); fft(g3,lim,1);
for(int i=0;i<lim;i++)	h[i] = f3[i] * g1[i]+ f1[i] * g3[i];
for(int i=0;i<lim;i++)	h[i] = h[i] - cpx(2.0,0.0)*f2[i]*g2[i];
fft(h,lim,-1);
//printf("%lf\n",ful);
//for(int i=0;i<lim;i++)	printf("%lf ",h[i].x);
for(int i=0;i<ls-lt+1;i++)
if(h[i+lt-1].x<0.5)	ans++,vec.push_back(i);
printf("%d\n",ans);
for(vector<int>::iterator it=vec.begin();it!=vec.end();it++)
printf("%d ",(*it)+1);
return 0;
}
/**
bbabaababaaaaabaaaaaaaabaaabbbabaaabbabaabbbbabbbbbbabbaabbbababababbbbbbaaabaaabbbbbaabbbaabbbbabab
a?aba?abba
*/

posted @ 2018-12-26 16:02  寒雨微凝  阅读(63)  评论(0编辑  收藏  举报