题意:海明距离的定义:两个相同长度的字符串中不同的字符数.现给出母串A和模式串B,求A中有多少与B海明距离<=k的不同子串

分析:将字符a视作1,b视作0.则A与B中都是a的位置乘积是1.现将B逆置,并设B的长度为n,令\(C(n+k-1)= \sum_{i=0}^{n-1}A_{i+k}*B_{n-i-1}\),表示母串A中从位置k开始,长度为n的子串与B中字符都是'a'的位置的数目,可以通过FFT运算得到.再对字符'b'做一次同样的运算,\(ans[i]\)统计母串A中以i结尾的子串与B相同字符的个数.

设A的长度为m,则一共有\(m-n+1\)个子串,若\(n-ans[i] \leq k\),则盖子串符合条件,但本题需要求不同的子串,所以将母串哈希掉,将符合要求的哈希值存在集合中去重.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 4e5 + 10;
const double PI = acos(-1.0);
struct Complex{
double x, y;
inline Complex operator+(const Complex b) const {
return (Complex){x +b.x,y + b.y};
}
inline Complex operator-(const Complex b) const {
return (Complex){x -b.x,y - b.y};
}
inline Complex operator*(const Complex b) const {
return (Complex){x *b.x -y * b.y,x * b.y + y * b.x};
}
} va[MAXN * 2 + MAXN / 2], vb[MAXN * 2 + MAXN / 2];
int lenth = 1, rev[MAXN * 2 + MAXN / 2];
int N, M; // f 和 g 的数量
//f g和 的系数
// 卷积结果
// 大数乘积
int f[MAXN],g[MAXN];
vector<LL> conv;
vector<LL> multi;
//f g
void init()
{
int tim = 0;
lenth = 1;
conv.clear(), multi.clear();
memset(va, 0, sizeof va);
memset(vb, 0, sizeof vb);
while (lenth <= N + M - 2)
lenth <<= 1, tim++;
for (int i = 0; i < lenth; i++)
rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (tim - 1));
}
void FFT(Complex *A, const int fla)
{
for (int i = 0; i < lenth; i++){
if (i < rev[i]){
swap(A[i], A[rev[i]]);
}
}
for (int i = 1; i < lenth; i <<= 1){
const Complex w = (Complex){cos(PI / i), fla * sin(PI / i)};
for (int j = 0; j < lenth; j += (i << 1)){
Complex K = (Complex){1, 0};
for (int k = 0; k < i; k++, K = K * w){
const Complex x = A[j + k], y = K * A[j + k + i];
A[j + k] = x + y;
A[j + k + i] = x - y;
}
}
}
}
void getConv(){ //求多项式
init();
for (int i = 0; i < N; i++)
va[i].x = f[i];
for (int i = 0; i < M; i++)
vb[i].x = g[i];
FFT(va, 1), FFT(vb, 1);
for (int i = 0; i < lenth; i++)
va[i] = va[i] * vb[i];
FFT(va, -1);
for (int i = 0; i <= N + M - 2; i++)
conv.push_back((LL)(va[i].x / lenth + 0.5));
} char s1[100005],s2[100005];
LL res[MAXN]; const int seed = 3;
LL dig[MAXN],Hash[MAXN];
set<LL> dp; void pre(){
dig[0] =1;
for(int i=1;i<=100005;++i){
dig[i] = dig[i-1]*seed;
}
} LL getHash(int L ,int R){
if(L==0) return Hash[R];
return Hash[R] - Hash[L-1]*dig[R-L+1];
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
pre();
int k,cas=1;
while(scanf("%d",&k)==1){
if(k==-1) break;
memset(res,0,sizeof(res));
dp.clear();
scanf("%s",s1);
scanf("%s",s2);
int len1 = strlen(s1), len2 = strlen(s2);
N = len1, M = len2;
for(int i=0;i<len1;++i){
if(s1[i]=='a') f[i] = 1;
else f[i] = 0;
}
for(int i=0;i<len2;++i){
if(s2[len2-i-1]=='a') g[i] = 1;
else g[i] = 0;
}
getConv();
int sz =conv.size();
for(int i=len2-1;i<sz;++i){
res[i] += conv[i];
} for(int i=0;i<len1;++i){
if(s1[i]=='b') f[i] = 1;
else f[i] = 0;
}
for(int i=0;i<len2;++i){
if(s2[len2-i-1]=='b') g[i] = 1;
else g[i] = 0;
}
getConv();
sz =conv.size();
for(int i=len2-1;i<sz;++i){
res[i] += conv[i];
} //Hash
Hash[0] = s1[0]-'a'+1;
for(int i=1;i<len1;++i){
Hash[i] = Hash[i-1]*seed + s1[i]-'a'+1;
} for(int i=len2-1;i<len1;++i){
LL now = getHash(i-len2+1,i);
if(len2-res[i]<=k){
dp.insert(now);
}
}
printf("Case %d: %d\n",cas++,(int)dp.size());
}
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

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

  3. 1087: Common Substrings (哈希)

    1087: Common Substrings Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/ ...

  4. UVALive - 6893 The Big Painting 字符串哈希

    题目链接: http://acm.hust.edu.cn/vjudge/problem/129730 The Big Painting Time Limit: 5000MS 题意 给你一个模板串和待匹 ...

  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. 图像检索(6):局部敏感哈希索引(LSH)

    图像检索中,对一幅图像编码后的向量的维度是很高.以VLAD为例,基于SIFT特征点,设视觉词汇表的大小为256,那么一幅图像编码后的VLAD向量的长度为$128 \times 256 = 32768 ...

  8. CC countari & 分块+FFT

    题意: 求一个序列中顺序的长度为3的等差数列. SOL: 对于这种计数问题都是用个数的卷积来进行统计.然而对于这个题有顺序的限制,不好直接统计,于是竟然可以分块?惊为天人... 考虑分块以后的序列: ...

  9. HDU4609 & FFT

    关于这道题请移步kuangbin爷的blog:http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html 感觉我一辈子也不能写出这么 ...

随机推荐

  1. hdu 1140:War on Weather(计算几何,水题)

    War on Weather Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. 利用新浪云平台(SAE) 搭建 HUSTOJ 简易教程

    前言: OnlineJudge(OJ)是一种代码在线判定平台,这里有许多的编程题目供你选择,你可以选择题目提交代码,OJ会自动返回你的代码的判定结果.是一种很方便的编程.算法练习平台.详情可见:百度百 ...

  3. leetcode 153: Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. ios开发之--CGRect/CGSize/CGPoint/CGVector/CGAffineTransform/UIEdgeInsets/UIOffset和NSString之间的转换

    仅做记录,一个函数和字符串之间的互相转换 方法如下: UIKIT_EXTERN NSString *NSStringFromCGPoint(CGPoint point); UIKIT_EXTERN N ...

  5. Android 蓝牙学习

    Android 蓝牙学习 学习缘由 上个礼拜公司要开发个简单的五子棋游戏!其中一个需求就是支持蓝牙对战!所以苦逼的我学习蓝牙方面的知识了! 简介 Bluetooth是目前使用最广泛的无线通讯协议,近距 ...

  6. 匿名(无账号密码)从ftp服务器下载文件

    public static String downFile(String ip,String ftpFileName,String savePath,String fileName) { FTPCli ...

  7. LeetCode 笔记系列五 Generate Parentheses

    题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...

  8. SharePoint PerformancePoint开发实例

    前言 由于工作的原因,有一段时间没有发新的随笔了,最近使用了SharePoint PerformancePoint做了一些报表,与大家分享经验. 本文完全原创,转载请说明出处,希望对大家有用. 阅读目 ...

  9. TCP requires two packet transfers to set up the connection before it can send data

    wHTTP重用现存连接来减少TCP建立时延. HTTP The Definitive Guide 4.2.3 TCP Connection Handshake Delays When you set ...

  10. 正则表达式验证合法的IP地址

    IPv4地址 最初设计互联网络时,为了便于寻址和层次化构造网络,每个IP地址包括两个标识码(ID),即网络ID和主机ID.同一个物理网络上的所有主机都使用同一个网络ID,网络上的一个主机(包括网络上的 ...