4259. Incomplete string

Keywords: PHP

Portal

Use $FFT $for string matching, immortal operation...

For two strings, $A, $B, $dis (A,B) = sum_i (A_i-B_i) ^ 2$

Obviously, if and only if $A=B, $dis(A,B)=0$

There is also a requirement that'*'be wildcards, so the question of $dis (A, B)= sum_i((A_i-B_i)^ 2 [A_i!='*'] [B_i!='*].$

It is found that if'*'is set to $0, then $dis (A, B)= sum_i((A_i-B_i)^ 2A_iB_i)$

For this question, let the $A $string be a template string, then for the $B $string, $[i-m+1,i]$, if matched

Then $dis [i]= sum {j=0} ^ {m-1} ((A_j-B_{i-m+1+j}) ^ 2A_jB_{i-m+1+j})$

Turn $A into $A'$and fill in the following zero until it is the same length as $B, then $dis [i]= sum {j=0} ^ {m-1} ((A'_j-B_{i-j}) ^ 2A'_jB_{i-j})$

Expansion

$dis[i]=\sum_{j=0}^{m-1}{A'}_{j}^{3}B_{i-j}-2\sum_{j=0}^{m-1}{A'}_{j}^{2}B_{i-j}^2+\sum_{j=0}^{m-1}{A'}_{j}B_{i-j}^{3}$

Then three segments of $FFT $would be fine.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef double db;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=2e6+7;
const db pi=acos(-1.0);
struct CP {
    db x,y;
    CP (db xx=0,db yy=0) { x=xx,y=yy; }
    inline CP operator + (const CP &tmp) const { return CP(x+tmp.x,y+tmp.y); }
    inline CP operator - (const CP &tmp) const { return CP(x-tmp.x,y-tmp.y); }
    inline CP operator * (const CP &tmp) const { return CP(x*tmp.x-y*tmp.y,x*tmp.y+y*tmp.x); }
}A[N],B[N],C[N];
int n,m,p[N];
void FFT(CP *A,int len,int type)
{
    for(int i=0;i<len;i++) if(i<p[i]) swap(A[i],A[p[i]]);
    for(int mid=1;mid<len;mid<<=1)
    {
        CP wn(cos(pi/mid),type*sin(pi/mid));
        for(int R=mid<<1,j=0;j<len;j+=R)
        {
            CP w(1,0);
            for(int k=0;k<mid;k++,w=w*wn)
            {
                CP x=A[j+k],y=w*A[j+mid+k];
                A[j+k]=x+y;
                A[j+mid+k]=x-y;
            }
        }
    }
}
char sa[N],sb[N];
int a[N],b[N],ans[N],Top;
int main()
{
    m=read(),n=read();
    scanf("%s%s",sa,sb);
    for(int i=0;i<m;i++) a[m-i-1]= sa[i]!='*' ? sa[i]-'a'+1 : 0;
    for(int i=0;i<n;i++) b[i]= sb[i]!='*' ? sb[i]-'a'+1 : 0;
    int len=1,tot=0;
    while(len<n+m-1) len<<=1,tot++;
    for(int i=0;i<len;i++) p[i]=(p[i>>1]>>1) | ((i&1)<<(tot-1));

    for(int i=0;i<=len;i++) A[i].x=a[i]*a[i]*a[i],B[i].x=b[i];//here AB No value, directly to x Assignment is OK
    FFT(A,len,1); FFT(B,len,1);
    for(int i=0;i<=len;i++) C[i]=A[i]*B[i];

    for(int i=0;i<=len;i++) A[i]=CP(a[i]*a[i],0),B[i]=CP(b[i]*b[i],0);//Be careful AB At this point, there is a value to initialize
    FFT(A,len,1); FFT(B,len,1);
    for(int i=0;i<=len;i++) C[i]=C[i]-A[i]*B[i]*CP(2,0);

    for(int i=0;i<=len;i++) A[i]=CP(a[i],0),B[i]=CP(b[i]*b[i]*b[i],0);
    FFT(A,len,1); FFT(B,len,1);
    for(int i=0;i<=len;i++) C[i]=C[i]+A[i]*B[i];

    FFT(C,len,-1);
    for(int i=m-1;i<n;i++) if(C[i].x/len<0.5) ans[++Top]=i-m+2;
    printf("%d\n",Top);
    for(int i=1;i<=Top;i++) printf("%d ",ans[i]);
    if(Top) printf("\n");
    return 0;
}

Posted by jasonman1 on Wed, 31 Jul 2019 02:58:41 -0700