UVALive4671   K-neighbor substrings   给定一个两个字符串A和B B为模式串。问A中有多少不同子串与B的距离小于k 所谓距离就是不同位的个数。

由于字符串只包含a和b 我们可以换做0和1 将B反转 进行大整数乘法(卷积) 就可以轻松得出不同的位数。只有FFT能在nlogn时间内完成大整数乘法。

要求不同的子串,再进行一次字符串散列即可。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<queue>
#include<stack>
#include<complex>
//使用FFT时注意精度问题和数组大小
using namespace std;
typedef long long int LL;
const double pi=acos(-1.0);
struct Complex {
double r, i;
Complex(double _r, double _i) {
r = _r;
i = _i;
}
double real()
{
return r;
}
double ii()
{
return i;
}
Complex operator + (const Complex &c) {
return Complex(c.r + r, c.i + i);
}
Complex operator - (const Complex &c) {
return Complex(r - c.r, i - c.i);
}
Complex operator * (const Complex &c) {
return Complex(c.r * r - c.i * i, c.r * i + c.i * r);
}
Complex operator / (const int &c) {
return Complex(r / c, i / c);
}
Complex(){}
};
void build(Complex _P[],Complex P[],LL n,LL m,LL curr,LL &cnt)
{
if(n==m){_P[curr]=P[cnt++];}
else {build(_P,P,n,m*2,curr,cnt);build(_P,P,n,m*2,curr+m,cnt);} }
const LL maxn=300000;
void FFT(Complex P[],LL n,LL oper)//返回结果向左靠齐 最后结果除n
{
static Complex _P[maxn];
LL cnt=0;
build(_P,P,n,1,0,cnt);copy(_P,_P+n,P);
for(LL d=0;(1<<d)<n;d++)
{
LL m=1<<d;
LL m2=m*2;
double p0=pi/m*oper;
Complex unit_p0=Complex(cos(p0),sin(p0));
for(LL i=0;i<n;i+=m2)
{
Complex unit=Complex(1,0);
for(LL j=0;j<m;j++)
{
Complex &P1=P[i+j+m],&P2=P[i+j];
Complex t=unit*P1;
P1=P2-t;
P2=P2+t;
unit=unit*unit_p0;
}
}
}
}
void himult(Complex p1[],Complex p2[],LL n,Complex ans[])
{ FFT(p1,n,1);FFT(p2,n,1);
for(LL i=0;i<=n;i++)
ans[i]=p1[i]*p2[i];
FFT(ans,n,-1);
}
char a[131072*2],b[131072*2];
Complex av[131072*4],bv[131072*2+1],ans1[131072*2+1];
unsigned long long int ha[200000],xp[200000];
const int seed=3;
set<unsigned long long int >vise;
bool hash(int l,int r)
{
unsigned long long int nowv= ha[r+1]-ha[l]*xp[r-l+1];
if(vise.count(nowv)==0){vise.insert(nowv);return false;}
else return true;
}
int main()
{
freopen("t.txt","r",stdin);
xp[0]=1;
for(int i=1;i<=100000;i++)
xp[i]=xp[i-1]*seed;
int ii=0;
while(1)
{ ii++;
vise.clear();
int k;
scanf("%d",&k);
if(k==-1)break;
memset(a,0,sizeof(a));memset(b,0,sizeof(b));
scanf("%s%s",&a,&b);
int la=strlen(a),lb=strlen(b);
int len=1;
while(len<=la+lb)len<<=1;
for(int i=0;i<la;i++)
av[i]=Complex(a[i]=='a'?1:-1,0);
for(int i=la;i<=len;i++)av[i]=Complex(0,0);
for(int i=0;i<lb;i++)
bv[lb-i-1]=Complex(b[i]=='a'?1:-1,0);
for(int i=lb;i<=len;i++)
bv[i]=Complex(0,0);
memset(ans1,0,sizeof(ans1));
himult(av,bv,len,ans1);
int anss=0;
ha[0]=0;
for(int i=0;i<la;i++)
ha[i+1]=ha[i]*seed+a[i];
for(int i=0;i+lb<=la;i++)
{
if(hash(i,i+lb-1))continue;
int nue=(int)(ans1[i+lb-1].r/len+0.5);
if(lb-nue<=k*2)anss++;
}
printf("Case %d: %d\n",ii,anss);
}
return 0;
}

  

UVALive 4671 K-neighbor substrings 巧用FFT的更多相关文章

  1. UVALive 7721 K - 2-ME Set 集合dp,所有数的位或来表示状态。

    /** 题目:UVALive 7721 K - 2-ME Set 链接:https://vjudge.net/problem/UVALive-7721 题意:给定n个数,从中取出一个集合,至少包含两个 ...

  2. UVALive - 4671 K-neighbor substrings (FFT+哈希)

    题意:海明距离的定义:两个相同长度的字符串中不同的字符数.现给出母串A和模式串B,求A中有多少与B海明距离<=k的不同子串 分析:将字符a视作1,b视作0.则A与B中都是a的位置乘积是1.现将B ...

  3. UVALive - 6886 Golf Bot 多项式乘法(FFT)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/129724 Golf Bot Time Limit: 15000MS 题意 给你n个数,m个查询,对于每个查询 ...

  4. loj #6247. 九个太阳 k次单位根 神仙构造 FFT求和原理

    LINK:九个太阳 不可做系列. 构造比较神仙. 考虑FFT的求和原理有 \(\frac{1}{k}\sum_{j=0}^{k-1}(w_k^j)^n=[k|n]\) 带入这道题的式子. 有\(\su ...

  5. UVALive - 6257 K - Chemist's vows 【DFS】【BFS】【DP】

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. CodeForces - 528D Fuzzy Search (FFT求子串匹配)

    题意:求母串中可以匹配模式串的子串的个数,但是每一位i的字符可以左右偏移k个位置. 分析:类似于 UVALive -4671. 用FFT求出每个字符成功匹配的个数.因为字符可以偏移k个单位,先用尺取法 ...

  7. 快速傅里叶变换FFT

    多项式乘法 #include <cstdio> #include <cmath> #include <algorithm> #include <cstdlib ...

  8. 用于ARM上的FFT与IFFT源代码(C语言,不依赖特定平台)(转)

    源:用于ARM上的FFT与IFFT源代码(C语言,不依赖特定平台) 代码在2011年全国电子大赛结束后(2011年9月3日)发布,多个版本,注释详细. /*********************** ...

  9. FFT\NTT总结

    学了好久,终于基本弄明白了 推荐两个博客: 戳我 戳我 再推荐几本书: <ACM/ICPC算法基础训练教程> <组合数学>(清华大学出版社) <高中数学选修> 预备 ...

随机推荐

  1. 用Docker构建MySQL镜像

    构建MySQL镜像 本文目的不仅仅是创建一个MySQL的镜像,而是在其基础上再实现启动过程中自动导入数据及数据库用户的权限设置,并且在新创建出来的容器里自动启动MySQL服务接受外部连接,主要是通过D ...

  2. HDU - 5952 Counting Cliques(dfs搜索)

    题目: A clique is a complete graph, in which there is an edge between every pair of the vertices. Give ...

  3. ubuntu 14.04 挂载window共享目录

    (1) 先在ubuntu系统里,新建一个目录用于挂载,目录假设为 /mnt/win: sudo mkdir /mnt/win (2)在windows系统,共享出一个文件夹,共享名称假设为www sud ...

  4. linux whereis-查找二进制程序、代码等相关文件路径

    推荐:更多Linux 文件查找和比较 命令关注:linux命令大全 whereis命令用来定位指令的二进制程序.源代码文件和man手册页等相关文件的路径. whereis命令只能用于程序名的搜索,而且 ...

  5. js之获取html标签的值

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 数据分布vs聚类-数据预处理技巧-对数变换

    对于原始数据分布倾斜 利用统计或数学变换来减轻数据分布倾斜的影响.使原本密集的区间的值尽可能的分散, 原本分散的区间的值尽量的聚合. Log变换通常用来创建单调的数据变换.它的主要作用在于帮助稳定方差 ...

  7. java方法的虚分派和方法表

    java:方法的虚分派(virtual dispatch)和方法表(method table) Java方法调用的虚分派 虚分配(Virtual Dispatch) 首先从字节码中对方法的调用说起.J ...

  8. 简单svg动画

    一.将svg嵌入到html中 svg是指可伸缩矢量图形,它使用XML格式定义图像.在html中可以使用<svg>标签直接嵌入svg代码,例如: <svg version=" ...

  9. The Pilots Brothers' refrigerator DFS+枚举

    Description The game “The Pilots Brothers: following the stripy elephant” has a quest where a player ...

  10. Codeforces Round #391(div 1+2)

    A =w= B QuQ C 题意:有n个体育场,每个体育场有一些小精灵,一共m种小精灵(n<=1e5,m<=1e6),可以将数字全为i的精灵进化成j(可以互相进化也可以选择不进化),问有多 ...