正题
题目链接:https://www.luogu.com.cn/problem/P4173
题目大意
给出两个字符串\(S,T\),其中包含小写字母和一些\(?\),\(?\)可以匹配任何字符。
求有多少个\(p\)使得\(T_{0\sim |t|-1}=S_{p\sim p+|t|-1}\)
解题思路
如果不考虑\(?\),我们可以用做差法来匹配两个字符,构造匹配函数
\[f(x)=\sum_{i=0}^{m}(T_i-S_{x+i})^2 \]这样若\(f(x)=0\)证明它们在位置\(x\)处匹配。
但是现在有\(?\),也就是要跳过有\(?\)的位置,定义\(?\)的值为\(0\),然后改一下匹配函数
\[f(x)=\sum_{i=0}^{m}(T_i-S_{x+i})^2T_iS_{x+i} \]展开二次项
\[f(x)=\sum_{i=0}^{m}T_i^3S_{x+i}-2T_{i}^2S_{x+i}^2+T_iS_{x+i}^3 \]把\(T\)反过来就是\(\sum_{i=0}^{m}T_{m-i-1}^3S_{x+i}-2T_{m-i-1}^2S_{x+i}^2+T_{m-i-1}S_{x+i}^3\)
然后有三个式子卷积之后加起来就好了。
常数极大,开\(\text{-O2}\)才能过。时间复杂度\(O(n\log n)\)
code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define int long long
using namespace std;
const int N=1e6+2e5;
const double Pi=acos(-1);
struct complex{
double x,y;
complex(double xx=0,double yy=0)
{x=xx;y=yy;return;}
};
complex operator+(complex a,complex b)
{return complex(a.x+b.x,a.y+b.y);}
complex operator-(complex a,complex b)
{return complex(a.x-b.x,a.y-b.y);}
complex operator*(complex a,complex b)
{return complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
struct poly{
complex a[N];
}F[3],G[3];
int n,m,r[N],k;
char s[N],t[N];
queue<int> q;
void FFT(complex *f,int op,int n){
for(int i=0;i<n;i++)
if(i<r[i])swap(f[i],f[r[i]]);
for(int p=2;p<=n;p<<=1){
int len=p>>1;
complex tmp(cos(Pi/len),sin(Pi/len)*op);
for(int k=0;k<n;k+=p){
complex buf(1,0);
for(int i=k;i<k+len;i++){
complex tt=buf*f[i+len];
f[i+len]=f[i]-tt;
f[i]=f[i]+tt;
buf=buf*tmp;
}
}
}
if(op==-1)
for(int i=0;i<n;i++)
f[i].x/=(double)n;
return;
}
void mul(poly &a,poly &b){
FFT(a.a,1,k);FFT(b.a,1,k);
for(int i=0;i<k;i++)
a.a[i]=a.a[i]*b.a[i];
FFT(a.a,-1,k);
return;
}
signed main()
{
scanf("%d%d",&m,&n);
scanf("%s",t);scanf("%s",s);
for(int i=0;i<n;i++){
char c=s[i];int z=s[i]-'a'+1;
if(c=='*')z=0;
F[0].a[i]=complex(z*z*z,0);
F[1].a[i]=complex(z*z,0);
F[2].a[i]=complex(z,0);
}
for(int i=0;i<m;i++){
char c=t[m-i-1];int z=c-'a'+1;
if(c=='*')z=0;
G[0].a[i]=complex(z,0);
G[1].a[i]=complex(z*z,0);
G[2].a[i]=complex(z*z*z,0);
}
k=1;for(k=1;k<=2*n;)k<<=1;
for(int i=0;i<k;i++)
r[i]=(r[i>>1]>>1)|((i&1)?(k>>1):0);
mul(F[0],G[0]);mul(F[1],G[1]);mul(F[2],G[2]);
for(int i=0;i<k;i++)
F[0].a[i].x=F[0].a[i].x-2.0*F[1].a[i].x+F[2].a[i].x;
k=0;
for(int i=0;i<=n-m;i++)
if(fabs(F[0].a[i+m-1].x)<0.5)
k++,q.push(i);
printf("%d\n",k);
while(!q.empty())
printf("%d ",q.front()+1),q.pop();
return 0;
}