DescriptionThe rabbits were playing two strings of games. Given two strings s and T, the rabbits want to know how many times T has appeared in S,where they appear respectively. Note that there may be "?" in T. Character, this character can match any character. Inputtwo lines of two strings, respectively, representing S and TOutputThe first line is a positive integer k, which indicates that T has appeared several times in SThe next K-line positive integers, respectively, represent the start position of T each time it appears in S. In order to output from small to large, the S subscript starts at 0.
Two strings A, B is equal (there is a wildcard in a) and only if Σ (A[i]-b[i]) 2b[i]=0, where A[i],b[i] is the corresponding number of the corresponding character, and the wildcard corresponds to 0
Flip S string and the T string with 0 and s equal length after the formula can be converted into convolution form with FFT calculation
#include <cstdio>#include<cstring>#include<cmath>#include<algorithm>intn,p;Const DoublePi=3.14159265358979323846;intr[524288];structcplx{Doubleb; Inline CPLX (DoubleR=0,DoubleI=0) {a=r;b=i;} Inline CPLXoperator+ (CPLX x) {returnCPLX (a+x.a,b+x.b);} Inline CPLXoperator-(CPLX x) {returnCPLX (a-x.a,b-x.b);} Inline CPLXoperator* (CPLX x) {returnCPLX (a*x.a-b*x.b,a*x.b+b*x.a);}} a[524288],b[524288];voidDFT (Cplx*a,intt) { for(intI=0; i<n;i++)if(i>R[i]) Std::swap (A[i],a[r[i]); for(intI=1; i<n;i<<=1) {CPLX w (cos (pi/i), T*sin (pi/i)); for(intj=0;j<n;j+=i<<1) {CPLX E (1), *b=a+j,*c=b+i; for(intk=0; k<i;k++,e=e*W) {CPLX x=b[k],y=c[k]*e; B[K]=x+y,c[k]=x-y; } } }}Chars1[200005],s2[200005];intv1[200005],v2[200005];inth1[200005],h2[200005];int as[200005],ap=0;intl1,l2,h3=0;intMain () {input:{scanf ("%s%s", S1,S2); L1=strlen (S1)-1; L2=strlen (S2)-1; for(intI=0; i<=l1;i++) v1[i]=s1[l1-i]-'a'+1; for(intI=0; i<=l2;i++) v2[i]=s2[i]!='?'? s2[i]-'a'+1:0; for(intI=0; i<=l2;i++) h3+=v2[i]*v2[i]*V2[i]; } init:{ for(n=2, p=0; n<=l1+1; n<<=1,++q); N<<=1,++Q; for(intI=1; i<n;i++) r[i]=r[i>>1]>>1| (i&1) <<P; } calc:{ for(intI=0; i<=l1;i++) A[I]=CPLX (v1[i]*V1[i]); for(intI=0; i<=l1;i++) b[i]=CPLX (V2[i]); for(inti=l1+1; i<n;i++) a[i]=b[i]=CPLX (); DFT (A,1), DFT (b,1); for(intI=0; i<n;i++) a[i]=a[i]*B[i]; DFT (A,-1); for(intI=0; i<=l1;i++) h1[i]= (int) (a[i].a/n+0.5); for(intI=0; i<=l1;i++) a[i]=CPLX (V1[i]); for(intI=0; i<=l1;i++) B[I]=CPLX (v2[i]*V2[i]); for(inti=l1+1; i<n;i++) A[I]=B[I]=CPLX (0,0); DFT (A,1), DFT (b,1); for(intI=0; i<n;i++) a[i]=a[i]*B[i]; DFT (A,-1); for(intI=0; i<=l1;i++) h2[i]= (int) (a[i].a/n+0.5); } output:{ for(inti=l1;i>=l2;i--)if(h1[i]-2*h2[i]+h3==0) as[ap++]=l1-i; printf ("%d\n", AP); for(intI=0; i<ap;i++) printf ("%d\n", as[i]); } return 0;}
bzoj4403 Two strings