# bzoj4259 残缺的字符串

http://www.elijahqi.win/archives/3098
Description

Input

Output

Sample Input

3 7
a*b
aebr*ob
Sample Output

2
1 5
HINT

Source

By Claris

orz fft 字符串匹配 好那么

#include<cmath>
#include<cstdio>
#include<cstring>
#define pi acos(-1)
#include<algorithm>
using namespace std;
const int N=300030;
struct C{
double r,i;
inline friend C operator +(const C &a,const C &b){return (C){a.r+b.r,a.i+b.i};}
inline friend C operator -(const C &a,const C &b){return (C){a.r-b.r,a.i-b.i};}
inline friend C operator *(const C &a,const C &b){return (C){a.r*b.r-a.i*b.i,a.r*b.i+a.i*b.r};}
inline void operator *=(const C &a){*this=*this*a;}
}a[N<<2],b[N<<2],c[N<<2];
char ss1[N],s2[N];
int pend[N],ans,n,m,a1[N<<2],a2[N<<2],R[N<<2];
inline void fft(C *x,int f){
for (int i=0;i<n;++i) if (i<R[i]) swap(x[i],x[R[i]]);
for (int i=1;i<n;i<<=1){
C wn=(C){cos(pi/i),f*sin(pi/i)};
for (int j=0;j<n;j+=i<<1){
C w=(C){1,0},t1,t2;
for (int k=0;k<i;++k,w*=wn)
t1=x[j+k],t2=x[j+i+k]*w,x[j+k]=t1+t2,x[j+i+k]=t1-t2;
}
}if (f==-1) for (int i=0;i<n;++i) x[i].r/=n;
}
int main(){
freopen("bzoj4259.in","r",stdin);
scanf("%d%d",&m,&n);scanf("%s",ss1);scanf("%s",s2);int nn=n;
for (int i=0;i<m;++i) a1[i]=ss1[m-i-1]!='*'?ss1[m-i-1]-'a'+1:0;
for (int i=0;i<n;++i) a2[i]=s2[i]!='*'?s2[i]-'a'+1:0;m+=n;int t=0;
for (n=1;n<m;n<<=1,++t);for (int i=0;i<n;++i) R[i]=(R[i>>1]>>1)|(i&1)<<t-1;
for (int i=0;i<n;++i) a[i]=(C){a1[i]*a1[i]*a1[i],0},b[i]=(C){a2[i],0};
fft(a,1);fft(b,1);for (int i=0;i<n;++i) c[i]=c[i]+a[i]*b[i];m-=nn;
for (int i=0;i<n;++i) a[i]=(C){a1[i]*a1[i],0},b[i]=(C){a2[i]*a2[i],0};
fft(a,1);fft(b,1);for (int i=0;i<n;++i) c[i]=c[i]-(C){2,0}*a[i]*b[i];
for (int i=0;i<n;++i) a[i]=(C){a1[i],0},b[i]=(C){a2[i]*a2[i]*a2[i],0};
fft(a,1);fft(b,1);for (int i=0;i<n;++i) c[i]=c[i]+a[i]*b[i];fft(c,-1);
for (int i=m-1;i<nn;++i) if (c[i].r<0.5) ++ans,pend[ans]=i+1-m+1;
printf("%d\n",ans);for (int i=1;i<=ans;++i) printf("%d ",pend[i]);
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120