# 【刷题】BZOJ 4259 残缺的字符串

3 7
a*b
aebr*ob

2
1 5

## Solution

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=1<<20;
const db Pi=acos(-1.0);
int n1,n2,n,m,cnt,rev[MAXN],ans[MAXN],nt;
char s1[MAXN],s2[MAXN];
struct Complex{
db real,imag;
inline Complex operator + (const Complex &A) const {
return (Complex){real+A.real,imag+A.imag};
};
inline Complex operator - (const Complex &A) const {
return (Complex){real-A.real,imag-A.imag};
};
inline Complex operator * (const Complex &A) const {
return (Complex){real*A.real-imag*A.imag,imag*A.real+real*A.imag};
};
};
Complex x[MAXN],x2[MAXN],x3[MAXN],y[MAXN],y2[MAXN],y3[MAXN];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void FFT(Complex *A,int tp)
{
for(register int i=0;i<n;++i)
if(i<rev[i])std::swap(A[i],A[rev[i]]);
for(register int l=2;l<=n;l<<=1)
{
Complex wn=(Complex){cos(2*Pi/l),sin(tp*2*Pi/l)};
for(register int i=0;i<n;i+=l)
{
Complex w=(Complex){1,0};
for(register int j=0;j<(l>>1);++j)
{
Complex A1=A[i+j],A2=w*A[i+j+(l>>1)];
A[i+j]=A1+A2,A[i+j+(l>>1)]=A1-A2;
w=w*wn;
}
}
}
}
int main()
{
m=n1+n2-1;
scanf("%s",s1);scanf("%s",s2);
for(register int i=0;i<n1;++i)
{
x[n1-i-1].real=(s1[i]=='*'?0:s1[i]-'a'+1);
x2[n1-i-1].real=x[n1-i-1].real*x[n1-i-1].real*2;
x3[n1-i-1].real=x[n1-i-1].real*x[n1-i-1].real*x[n1-i-1].real;
}
for(register int i=0;i<n2;++i)
{
y[i].real=(s2[i]=='*'?0:s2[i]-'a'+1);
y2[i].real=y[i].real*y[i].real;
y3[i].real=y[i].real*y[i].real*y[i].real;
}
for(n=1;n<m;n<<=1)++cnt;
for(register int i=0;i<n;++i)rev[i]=(rev[i>>1]>>1)|((i&1)<<(cnt-1));
FFT(x,1);FFT(x2,1);FFT(x3,1);
FFT(y,1);FFT(y2,1);FFT(y3,1);
for(register int i=0;i<n;++i)x[i]=x3[i]*y[i]-x2[i]*y2[i]+x[i]*y3[i];
FFT(x,-1);
for(register int i=0;i<n2-n1+1;++i)
if((int)(x[n1+i-1].real/n+0.5)==0)ans[++nt]=i;
write(nt,'\n');
for(register int i=1;i<=nt;++i)write(ans[i]+1,' ');
puts("");
return 0;
}

posted @ 2018-06-13 20:24  HYJ_cnyali  阅读(116)  评论(0编辑  收藏  举报