bzoj 4503: 两个串

和上一题一样的,这不过这个去掉一个就可以,大概是酱紫 ∑(aj-bi)*bi 就好(此处a,b可以和原题不同)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #define LL long long
 6 #define pi acos(-1)
 7 using namespace std;
 8 
 9 const int N=100005;
10 
11 struct complex
12 {
13     double r,i;
14     complex (double x=0, double y=0) {r=x; i=y;}
15     complex operator + (const complex a) {return complex(r+a.r,i+a.i);}
16     complex operator - (const complex a) {return complex(r-a.r,i-a.i);}
17     complex operator * (const complex a) {return complex(r*a.r-i*a.i,r*a.i+i*a.r);}
18 }a[N<<2],b[N<<2],c1[N<<2],c2[N<<2],c3[N<<2];
19 
20 int n,len,lem,m,qwq;
21 int ans[N],ans1[N],ans2[N],ans3[N],A[N],B[N];
22 int rev[N<<2];
23 char s1[N],s2[N];
24 
25 void FFT(complex *a, int f)
26 {
27     for (int i=0; i<n; i++) if (rev[i]>i) swap(a[i],a[rev[i]]);
28     for (int h=2; h<=n; h<<=1)
29     {
30         complex wn(cos(2*pi*f/h),sin(2*pi*f/h));
31         for (int i=0; i<n; i+=h)
32         {
33             complex w(1,0);
34             for (int j=0; j<(h>>1); j++,w=w*wn)
35             {
36                 complex x=a[i+j],y=w*a[i+j+(h>>1)];
37                 a[i+j]=x+y;
38                 a[i+j+(h>>1)]=x-y;
39             }
40         }
41     }
42     if (f==-1) for (int i=0; i<n; i++) a[i].r/=(double)n;
43 }
44 
45 int main(int argc, char const *argv[])
46 {
47     scanf("%s",s1); scanf("%s",s2);
48     n=strlen(s1); m=strlen(s2); qwq=n;
49     for (int i=0; i<n; i++) A[i]=s1[i]-'a'+1;
50     for (int i=0; i<m; i++) B[i]=(s2[m-i-1]=='?')?0:s2[m-i-1]-'a'+1;
51     int orz=n<<1,L=0;
52     for (n=1; n<orz; n<<=1) L++;
53     for (int i=0; i<n; i++)
54         rev[i]=(rev[i>>1]>>1)|((i&1)?n>>1:0);
55 
56     for (int i=0; i<qwq; i++) a[i]=A[i]*A[i];
57     for (int i=0; i<m; i++) b[i]=B[i];
58     FFT(a,1); FFT(b,1);
59     for (int i=0; i<n; i++) c1[i]=a[i]*b[i];
60     FFT(c1,-1);
61     for (int i=0; i<qwq; i++) ans1[i]=(int)(c1[i].r+0.1);
62 
63     memset(a,0,sizeof(a));
64     memset(b,0,sizeof(b));
65     for (int i=0; i<qwq; i++) a[i]=1;
66     for (int i=0; i<m; i++) b[i]=B[i]*B[i]*B[i];
67     FFT(a,1); FFT(b,1);
68     for (int i=0; i<n; i++) c2[i]=a[i]*b[i];
69     FFT(c2,-1);
70     for (int i=0; i<qwq; i++) ans2[i]=(int)(c2[i].r+0.1);
71 
72     memset(a,0,sizeof(a));
73     memset(b,0,sizeof(b));
74     for (int i=0; i<qwq; i++) a[i]=A[i];
75     for (int i=0; i<m; i++) b[i]=B[i]*B[i];
76     FFT(a,1); FFT(b,1);
77     for (int i=0; i<n; i++) c3[i]=a[i]*b[i];
78     FFT(c3,-1);
79     for (int i=0; i<qwq; i++) ans3[i]=(int)(c3[i].r+0.1);
80 
81     for (int i=m-1; i<qwq; i++)
82             if (ans1[i]+ans2[i]-2*ans3[i]==0) ans[++ans[0]]=i;
83     printf("%d\n",ans[0]);
84     for (int i=1; i<=ans[0]; i++) printf("%d\n",ans[i]-m+1);
85     return 0;
86 }

 

posted @ 2017-04-19 15:37  ws_ccd  阅读(141)  评论(0编辑  收藏  举报