题意:海明距离的定义:两个相同长度的字符串中不同的字符数.现给出母串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\),则盖子串符合条件,但本题需要求不同的子串,所以将母串哈希掉,将符合要求的哈希值存在集合中去重.

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int MAXN = 4e5 + 10;
  5. const double PI = acos(-1.0);
  6. struct Complex{
  7. double x, y;
  8. inline Complex operator+(const Complex b) const {
  9. return (Complex){x +b.x,y + b.y};
  10. }
  11. inline Complex operator-(const Complex b) const {
  12. return (Complex){x -b.x,y - b.y};
  13. }
  14. inline Complex operator*(const Complex b) const {
  15. return (Complex){x *b.x -y * b.y,x * b.y + y * b.x};
  16. }
  17. } va[MAXN * 2 + MAXN / 2], vb[MAXN * 2 + MAXN / 2];
  18. int lenth = 1, rev[MAXN * 2 + MAXN / 2];
  19. int N, M; // f 和 g 的数量
  20. //f g和 的系数
  21. // 卷积结果
  22. // 大数乘积
  23. int f[MAXN],g[MAXN];
  24. vector<LL> conv;
  25. vector<LL> multi;
  26. //f g
  27. void init()
  28. {
  29. int tim = 0;
  30. lenth = 1;
  31. conv.clear(), multi.clear();
  32. memset(va, 0, sizeof va);
  33. memset(vb, 0, sizeof vb);
  34. while (lenth <= N + M - 2)
  35. lenth <<= 1, tim++;
  36. for (int i = 0; i < lenth; i++)
  37. rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (tim - 1));
  38. }
  39. void FFT(Complex *A, const int fla)
  40. {
  41. for (int i = 0; i < lenth; i++){
  42. if (i < rev[i]){
  43. swap(A[i], A[rev[i]]);
  44. }
  45. }
  46. for (int i = 1; i < lenth; i <<= 1){
  47. const Complex w = (Complex){cos(PI / i), fla * sin(PI / i)};
  48. for (int j = 0; j < lenth; j += (i << 1)){
  49. Complex K = (Complex){1, 0};
  50. for (int k = 0; k < i; k++, K = K * w){
  51. const Complex x = A[j + k], y = K * A[j + k + i];
  52. A[j + k] = x + y;
  53. A[j + k + i] = x - y;
  54. }
  55. }
  56. }
  57. }
  58. void getConv(){ //求多项式
  59. init();
  60. for (int i = 0; i < N; i++)
  61. va[i].x = f[i];
  62. for (int i = 0; i < M; i++)
  63. vb[i].x = g[i];
  64. FFT(va, 1), FFT(vb, 1);
  65. for (int i = 0; i < lenth; i++)
  66. va[i] = va[i] * vb[i];
  67. FFT(va, -1);
  68. for (int i = 0; i <= N + M - 2; i++)
  69. conv.push_back((LL)(va[i].x / lenth + 0.5));
  70. }
  71. char s1[100005],s2[100005];
  72. LL res[MAXN];
  73. const int seed = 3;
  74. LL dig[MAXN],Hash[MAXN];
  75. set<LL> dp;
  76. void pre(){
  77. dig[0] =1;
  78. for(int i=1;i<=100005;++i){
  79. dig[i] = dig[i-1]*seed;
  80. }
  81. }
  82. LL getHash(int L ,int R){
  83. if(L==0) return Hash[R];
  84. return Hash[R] - Hash[L-1]*dig[R-L+1];
  85. }
  86. int main()
  87. {
  88. #ifndef ONLINE_JUDGE
  89. freopen("in.txt","r",stdin);
  90. freopen("out.txt","w",stdout);
  91. #endif
  92. pre();
  93. int k,cas=1;
  94. while(scanf("%d",&k)==1){
  95. if(k==-1) break;
  96. memset(res,0,sizeof(res));
  97. dp.clear();
  98. scanf("%s",s1);
  99. scanf("%s",s2);
  100. int len1 = strlen(s1), len2 = strlen(s2);
  101. N = len1, M = len2;
  102. for(int i=0;i<len1;++i){
  103. if(s1[i]=='a') f[i] = 1;
  104. else f[i] = 0;
  105. }
  106. for(int i=0;i<len2;++i){
  107. if(s2[len2-i-1]=='a') g[i] = 1;
  108. else g[i] = 0;
  109. }
  110. getConv();
  111. int sz =conv.size();
  112. for(int i=len2-1;i<sz;++i){
  113. res[i] += conv[i];
  114. }
  115. for(int i=0;i<len1;++i){
  116. if(s1[i]=='b') f[i] = 1;
  117. else f[i] = 0;
  118. }
  119. for(int i=0;i<len2;++i){
  120. if(s2[len2-i-1]=='b') g[i] = 1;
  121. else g[i] = 0;
  122. }
  123. getConv();
  124. sz =conv.size();
  125. for(int i=len2-1;i<sz;++i){
  126. res[i] += conv[i];
  127. }
  128. //Hash
  129. Hash[0] = s1[0]-'a'+1;
  130. for(int i=1;i<len1;++i){
  131. Hash[i] = Hash[i-1]*seed + s1[i]-'a'+1;
  132. }
  133. for(int i=len2-1;i<len1;++i){
  134. LL now = getHash(i-len2+1,i);
  135. if(len2-res[i]<=k){
  136. dp.insert(now);
  137. }
  138. }
  139. printf("Case %d: %d\n",cas++,(int)dp.size());
  140. }
  141. return 0;
  142. }

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. 实例讲解JQuery中this和$(this)区别

    这篇文章主要介绍了实例讲解JQuery中this和$(this)的区别,this表示当前的上下文对象是一个html对象,可以调用html对象所拥有的属性和方法,$(this),代表的上下文对象是一个j ...

  2. 【代码备份】NLM插值

    文件路径: main.m: %% 测试函数 clc,clear all,close all; %输入的原始小图 ima_ori=double(imread('F:\Users\****n\Docume ...

  3. JAVASCRIPT 浏览器兼容性问题及解决方案列表

    JAVASCRIPT 浏览器兼容性问题及解决方案列表(1)获取HTML元素只兼容IE:document.all.hello hello 兼容所有: document.getElementById(“h ...

  4. iOS 圆角投影

    self.backgroundColor = [UIColor whiteColor]; self.layer.shadowColor = [UIColor lightGrayColor].CGCol ...

  5. 自定义控件_VIewPager显示多个Item

    一直以来想搞明白这个不完全的VIewPager是怎么做到的,有幸看到这片篇文章 有二种实现方法 1.设置的属性 1.clipChildren属性 2.setPageMargin 3.更新Item外界面 ...

  6. Echarts 的悬浮框tooltip显示自定义格式化

    最近做的项目用到echarts雷达图,但是由于地市过多,遇到悬浮框显示问题被遮住 如图: 可以看到上面从兴安开始数据就被遮住了 为了解决这个被遮住的悬浮框,达到tooltip自定义格式 完成后的效果如 ...

  7. 使用隧道技术进行C&C通信

    一.C&C通信 这里的C&C服务器指的是Command & Control Server--命令和控制服务器,说白了就是被控主机的遥控端.一般C&C节点分为两种,C&a ...

  8. Arduino开发版学习计划--直流电机

    代码来源:http://www.cnblogs.com/starsnow/p/4579547.html // --------------------------------------------- ...

  9. Python巨蟒全栈开发目录

    巨蟒python全栈开发-第一阶段 基础数据类型&基础 1.什么是计算机&&python的简介(待补充) 2.while循环&&格式化输出&&运 ...

  10. Python resources

    我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-python 是 vinta 发起维护的 Python 资源列表,内容包括:Web框架.网络 ...