UVALive4671:K-neighbor substrings (FFT+Hash)

题目传送门:https://cn.vjudge.net/problem/UVALive-4671


题目大意:给出两个长度不超过 105 ,仅由小写字母a,b组成的串A和B。设C为A中所有和B等长的子串的集合(注意,这个集合不能有相同的元素),问C中有多少个元素和B的汉明距离小于等于给定的值k。多组数据,以k=-1结束。字符串X,Y的汉明距离就是使得 X[i]Y[i] 的i的个数。


题目分析:看到Ab.Ever和tututu都切了这题,那我也来刷一下这道水题吧。

先假设相同的子串可以重复贡献答案,如何快速求出每个子串到B的汉明距离?由于字符串仅由a,b组成,不妨先令A中所有a=1,B中所有b=1,其余位置设为0,然后把B反过来做一次FFT。这样只有当某个子串的某个位置是a,B的对应位置是b的时候,才会贡献汉明距离,而且每个子串的总贡献都被存进了多项式的某个系数中。再将A中的b设为1,B中的a设为1,用同样的方法进行操作,加上前一次操作的贡献,即可得到每个子串到B的汉明距离。

现在题目要求我们去重,所以写个字符串双hash+hash表即可,注意每组数据做完后要反向清空hash表。这里我选了1333333331和998244353做模数,感觉一点问题都没有,不知道为什么Ab.Ever被模数卡了这么久-_-!!!。一开始WA了好几次,我以为自己也被模数卡了,结果发现自己直接输出了答案,没有按”Case X: Y”的格式来输出QAQ……


CODE:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>
using namespace std;

const int maxn=300000;
const double pi=acos(-1.0);

struct Complex
{
    double X,Y;
    Complex (double a=0.0,double b=0.0) : X(a),Y(b) {}
} ;

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);}

int Rev[maxn];
Complex A[maxn];
Complex B[maxn];

const long long M1=1333333331;
const long long M2=998244353;
const long long M3=1000000009;
const long long M4=1998585857;
const int M=1333331;
typedef long long LL;

struct data
{
    LL num1,num2;
} Hash[M];

LL val1[maxn];
LL val2[maxn];

LL w1[maxn];
LL w2[maxn];

int s1[maxn];
int s2[maxn];
char s[maxn];

bool vis[maxn];
int sum[maxn];

int n,m,k,Case=0;
int N,Lg;

bool Check(LL v1,LL v2)
{
    LL y=(v1*M3+v2*M4)%M;
    while ( Hash[y].num1!=-1 && ( Hash[y].num1!=v1 || Hash[y].num2!=v2 ) ) y=(y+1)%M;
    if (Hash[y].num1!=-1) return true;
    Hash[y].num1=v1;
    Hash[y].num2=v2;
    return false;
}

void DFT(Complex *a,double f)
{
    for (int i=0; i<N; i++)
        if (i<Rev[i]) swap(a[i],a[ Rev[i] ]);

    for (int len=2; len<=N; len<<=1)
    {
        int mid=len>>1;
        Complex e( cos( 2.0*pi/((double)len) ) , f*sin( 2.0*pi/((double)len) ) );
        for (Complex *p=a; p!=a+N; p+=len)
        {
            Complex wn(1.0,0.0);
            for (int i=0; i<mid; i++)
            {
                Complex temp=wn*p[mid+i];
                p[mid+i]=p[i]-temp;
                p[i]=p[i]+temp;
                wn=wn*e;
            }
        }
    }
}

void FFT()
{
    DFT(A,1.0);
    DFT(B,1.0);
    for (int i=0; i<N; i++) A[i]=A[i]*B[i];
    DFT(A,-1.0);
    for (int i=0; i<N; i++) A[i].X/=((double)N);
}

void Push_out(LL v1,LL v2)
{
    LL y=(v1*M3+v2*M4)%M;
    while ( Hash[y].num1==-1 || ( Hash[y].num1!=v1 || Hash[y].num2!=v2 ) ) y=(y+1)%M;
    Hash[y].num1=-1;
    Hash[y].num2=-1;
}

int main()
{
    freopen("4671.in","r",stdin);
    freopen("4671.out","w",stdout);

    w1[0]=w2[0]=1;
    for (int i=1; i<maxn; i++)
    {
        w1[i]=(w1[i-1]<<1)%M1;
        w2[i]=(w2[i-1]<<1)%M2;
    }
    for (int i=0; i<M; i++) Hash[i].num1=-1;

    scanf("%d",&k);
    while (k!=-1)
    {
        Case++;
        scanf("%s",s);
        n=strlen(s);
        for (int i=0; i<n; i++) s1[i]=s[i]-'a';
        scanf("%s",s);
        m=strlen(s);
        for (int i=0; i<m; i++) s2[i]=s[i]-'a';

        if (n<m)
        {
            printf("Case %d: 0\n",Case);
            scanf("%d",&k);
            continue;
        }

        val1[0]=val2[0]=0;
        for (int i=1; i<=n; i++)
        {
            int x=s1[i-1];
            val1[i]=((val1[i-1]<<1)|x)%M1;
            val2[i]=((val2[i-1]<<1)|x)%M2;
        }

        for (int i=m; i<=n; i++)
        {
            LL v1=(val1[i]-val1[i-m]*w1[m]%M1+M1)%M1;
            LL v2=(val2[i]-val2[i-m]*w2[m]%M2+M2)%M2;
            vis[i]=Check(v1,v2);
        }

        N=1,Lg=0;
        while (N<n+m) N<<=1,Lg++;
        for (int i=0; i<N; i++) Rev[i]=0;
        for (int i=0; i<N; i++)
            for (int j=0; j<Lg; j++)
                if ( i&(1<<j) ) Rev[i]|=( 1<<(Lg-j-1) );

        for (int i=0; i<n; i++)
            if (!s1[i]) A[i]=Complex(1.0,0.0);
            else A[i]=Complex(0.0,0.0);
        for (int i=0; i<m; i++)
            if (s2[i]) B[m-i-1]=Complex(1.0,0.0);
            else B[m-i-1]=Complex(0.0,0.0);
        for (int i=n; i<N; i++) A[i]=Complex(0.0,0.0);
        for (int i=m; i<N; i++) B[i]=Complex(0.0,0.0);

        FFT();

        for (int i=0; i<n; i++) sum[i+1]=(int)floor( A[i].X+0.5 );

        for (int i=0; i<n; i++)
            if (s1[i]) A[i]=Complex(1.0,0.0);
            else A[i]=Complex(0.0,0.0);
        for (int i=0; i<m; i++)
            if (!s2[i]) B[m-i-1]=Complex(1.0,0.0);
            else B[m-i-1]=Complex(0.0,0.0);
        for (int i=n; i<N; i++) A[i]=Complex(0.0,0.0);
        for (int i=m; i<N; i++) B[i]=Complex(0.0,0.0);

        FFT();

        for (int i=0; i<n; i++) sum[i+1]+=( (int)floor( A[i].X+0.5 ) );
        int ans=0;
        for (int i=m; i<=n; i++) if ( sum[i]<=k && !vis[i] ) ans++;
        printf("Case %d: %d\n",Case,ans);

        for (int i=m; i<=n; i++) if (!vis[i])
        {
            LL v1=(val1[i]-val1[i-m]*w1[m]%M1+M1)%M1;
            LL v2=(val2[i]-val2[i-m]*w2[m]%M2+M2)%M2;
            Push_out(v1,v2);
        }
        scanf("%d",&k);
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值