UVALive - 4671 K-neighbor substrings (FFT+哈希)
题意:海明距离的定义:两个相同长度的字符串中不同的字符数.现给出母串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+哈希)的更多相关文章
- UVALive 7721 K - 2-ME Set 集合dp,所有数的位或来表示状态。
/** 题目:UVALive 7721 K - 2-ME Set 链接:https://vjudge.net/problem/UVALive-7721 题意:给定n个数,从中取出一个集合,至少包含两个 ...
- UVALive 4671 K-neighbor substrings 巧用FFT
UVALive4671 K-neighbor substrings 给定一个两个字符串A和B B为模式串.问A中有多少不同子串与B的距离小于k 所谓距离就是不同位的个数. 由于字符串只包含a和 ...
- 1087: Common Substrings (哈希)
1087: Common Substrings Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/ ...
- UVALive - 6893 The Big Painting 字符串哈希
题目链接: http://acm.hust.edu.cn/vjudge/problem/129730 The Big Painting Time Limit: 5000MS 题意 给你一个模板串和待匹 ...
- UVALive - 6257 K - Chemist's vows 【DFS】【BFS】【DP】
题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- CodeForces - 528D Fuzzy Search (FFT求子串匹配)
题意:求母串中可以匹配模式串的子串的个数,但是每一位i的字符可以左右偏移k个位置. 分析:类似于 UVALive -4671. 用FFT求出每个字符成功匹配的个数.因为字符可以偏移k个单位,先用尺取法 ...
- 图像检索(6):局部敏感哈希索引(LSH)
图像检索中,对一幅图像编码后的向量的维度是很高.以VLAD为例,基于SIFT特征点,设视觉词汇表的大小为256,那么一幅图像编码后的VLAD向量的长度为$128 \times 256 = 32768 ...
- CC countari & 分块+FFT
题意: 求一个序列中顺序的长度为3的等差数列. SOL: 对于这种计数问题都是用个数的卷积来进行统计.然而对于这个题有顺序的限制,不好直接统计,于是竟然可以分块?惊为天人... 考虑分块以后的序列: ...
- HDU4609 & FFT
关于这道题请移步kuangbin爷的blog:http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html 感觉我一辈子也不能写出这么 ...
随机推荐
- 实例讲解JQuery中this和$(this)区别
这篇文章主要介绍了实例讲解JQuery中this和$(this)的区别,this表示当前的上下文对象是一个html对象,可以调用html对象所拥有的属性和方法,$(this),代表的上下文对象是一个j ...
- 【代码备份】NLM插值
文件路径: main.m: %% 测试函数 clc,clear all,close all; %输入的原始小图 ima_ori=double(imread('F:\Users\****n\Docume ...
- JAVASCRIPT 浏览器兼容性问题及解决方案列表
JAVASCRIPT 浏览器兼容性问题及解决方案列表(1)获取HTML元素只兼容IE:document.all.hello hello 兼容所有: document.getElementById(“h ...
- iOS 圆角投影
self.backgroundColor = [UIColor whiteColor]; self.layer.shadowColor = [UIColor lightGrayColor].CGCol ...
- 自定义控件_VIewPager显示多个Item
一直以来想搞明白这个不完全的VIewPager是怎么做到的,有幸看到这片篇文章 有二种实现方法 1.设置的属性 1.clipChildren属性 2.setPageMargin 3.更新Item外界面 ...
- Echarts 的悬浮框tooltip显示自定义格式化
最近做的项目用到echarts雷达图,但是由于地市过多,遇到悬浮框显示问题被遮住 如图: 可以看到上面从兴安开始数据就被遮住了 为了解决这个被遮住的悬浮框,达到tooltip自定义格式 完成后的效果如 ...
- 使用隧道技术进行C&C通信
一.C&C通信 这里的C&C服务器指的是Command & Control Server--命令和控制服务器,说白了就是被控主机的遥控端.一般C&C节点分为两种,C&a ...
- Arduino开发版学习计划--直流电机
代码来源:http://www.cnblogs.com/starsnow/p/4579547.html // --------------------------------------------- ...
- Python巨蟒全栈开发目录
巨蟒python全栈开发-第一阶段 基础数据类型&基础 1.什么是计算机&&python的简介(待补充) 2.while循环&&格式化输出&&运 ...
- Python resources
我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-python 是 vinta 发起维护的 Python 资源列表,内容包括:Web框架.网络 ...