uvalive 4671 - K-neighbor substrings 快速傅利叶变换

题目链接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2672


题目概述:

定以两个长度相等的串的距离为对应位置上两个字符不一样的数量。比如"abbab"和"bbabb"的距离为3.

两个串被称为K-neighbor的当且仅当它们的长度相等且距离不超过k,现在给你两个右a,b组成的字符串A,B,求A中所有子串中有多少个不同的子串和B是k-Neighboor。

A,B的长度100000.


解题思路:

假设两个长度相等为N的串A,B, 先将B翻转一下,然后就是要比较A[1]和B[N],A[2]和B[N-1],这和多项式对应的乘法很像。如果把字符在串中的位置看成指数,对应字符为a的系数为1,对应字符为b的系数为0,那么最终乘出来多项式指数为N+1的系数就是两个串中对应位置a相同的数量,对字符串B进行同样的处理,就可以算出两个字符串中对应位置a,b相同的数量。最后就是不同的子串,用hash处理一下即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <iostream>
#include <set>
using namespace std;

const double PI = acos(-1.0);
//复数结构体
struct complex
{
    double r,i;
    complex(double _r = 0.0,double _i = 0.0)
    {
        r = _r; i = _i;
    }
    complex operator +(const complex &b)
    {
        return complex(r+b.r,i+b.i);
    }
    complex operator -(const complex &b)
    {
        return complex(r-b.r,i-b.i);
    }
    complex operator *(const complex &b)
    {
        return complex(r*b.r-i*b.i,r*b.i+i*b.r);
    }
};
/*
 * 进行FFT和IFFT前的反转变换。
 * 位置i和 (i二进制反转后位置)互换
 * len必须去2的幂
 */
void change(complex y[],int len)
{
    int i,j,k;
    for(i = 1, j = len/2;i < len-1; i++)
    {
        if(i < j)swap(y[i],y[j]);
        //交换互为小标反转的元素,i<j保证交换一次
        //i做正常的+1,j左反转类型的+1,始终保持i和j是反转的
        k = len/2;
        while( j >= k)
        {
            j -= k;
            k /= 2;
        }
        if(j < k) j += k;
    }
}
/*
 * 做FFT
 * len必须为2^k形式,
 * on==1时是DFT,on==-1时是IDFT
 */
void fft(complex y[],int len,int on)
{
    change(y,len);
    for(int h = 2; h <= len; h <<= 1)
    {
        complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
        for(int j = 0;j < len;j+=h)
        {
            complex w(1,0);
            for(int k = j;k < j+h/2;k++)
            {
                complex u = y[k];
                complex t = w*y[k+h/2];
                y[k] = u+t;
                y[k+h/2] = u-t;
                w = w*wn;
            }
        }
    }
    if(on == -1)
        for(int i = 0;i < len;i++)
            y[i].r /= len;
}
/
typedef unsigned long long LL;
const int maxn = 100007;
const int MAGIC = 3;
const int M = 1e9+7;
complex x1[maxn*4],x2[maxn*4];
string s1,s2;
int sum1[maxn*4],sum2[maxn*4];
int k;
int len,len1,len2;
LL Hash[maxn],Pow[maxn];
set<LL> s;

void solve(int res[],char c){
    len1 = s1.length(),len2 = s2.length();
    len = 1;
    while(len < 2*len1 || len<2*len2) len <<= 1;
    for(int i=0;i<len1;i++){
        if(s1[i] == c) x1[i] = complex(1,0);
        else x1[i] = complex(0,0);
    }
    for(int i=len1;i<len;i++){
        x1[i] = complex(0,0);
    }
    for(int i=0;i<len2;i++){
        if(s2[len2 - i -1] == c) x2[i] = complex(1,0);
        else x2[i] = complex(0,0);
    }
    for(int i=len2;i<len;i++){
        x2[i] = complex(0,0);
    }
    //
    fft(x1,len,1);
    fft(x2,len,1);
    for(int i=0;i<len;i++) x1[i] = x1[i]*x2[i];
    fft(x1,len,-1);

    //cout << "len:" << len << endl;
    for(int i=0;i<len;i++){
        res[i] = (int) (x1[i].r + 0.5);
        //cout<< res[i] << " ";
    }
    //cout << endl;
}

int main(){
    int nCase = 1;
    Pow[0] = 1;
    for(int i=1;i<maxn;i++) Pow[i] = Pow[i-1]*MAGIC;
    while(cin >> k){
        if(k == -1) break;
        cin >> s1 >> s2;

        solve(sum1,'a');
        solve(sum2,'b');

        Hash[0] = 0;
        s.clear();
        for(int i=0;i<=len1;i++) Hash[i+1] = Hash[i]*MAGIC + s1[i];

        for(int i=len2-1;i<len1;i++){
            //cout << i <<" " << len2 + i<< endl;
            LL now = Hash[i+1] - Hash[i - len2 + 1]*Pow[len2];
            if(len2 - sum1[i] - sum2[i] <= k){
                s.insert(now);
            }
        }
        printf("Case %d: %d\n",nCase++,s.size());
    }
    return 0;
}
/**
0
aabbab
ab
1
aabbab
ab
2
aabba
ab
-1
**/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值