kmp

  1. 1 #include <algorithm>
  2. 2 #include <iostream>
  3. 3 #include <cstring>
  4. 4 #include <cstdio>
  5. 5
  6. 6 using namespace std;
  7. 7
  8. 8 struct KMP{
  9. 9 char y[1010];//主串
  10. 10 char x[1010];//模式串
  11. 11 int n,m;
  12. 12 int next[1010];
  13. 13
  14. 14 int init(){
  15. 15 scanf("%s%s",y,x);
  16. 16 n=strlen(y);
  17. 17 m=strlen(x);
  18. 18 kmp_pre();
  19. 19 //prekmp();
  20. 20 return 1;
  21. 21 }
  22. 22
  23. 23 void kmp_pre(){//生成next数组
  24. 24 int i,j;
  25. 25 j=next[0]=-1;
  26. 26 i=0;
  27. 27 while(i<m){
  28. 28 while(-1!=j&&x[i]!=x[j])j=next[j];
  29. 29 next[++i]=++j;
  30. 30 }
  31. 31 }
  32. 32
  33. 33 /*void prekmp(){
  34. 34 int i,j;
  35. 35 j=next[0]=-1;
  36. 36 i=0;
  37. 37 while(i<m){
  38. 38 while(-1!=j&&x[i]!=x[j])j=next[j];
  39. 39 if(x[++i]==x[++j])next[i]=next[j];
  40. 40 else next[i]=j;
  41. 41 }
  42. 42 }*/
  43. 43
  44. 44 int kmp_count(){
  45. 45 int i,j;
  46. 46 int ans;
  47. 47 i=j=ans=0;
  48. 48 while(i<n){
  49. 49 while(-1!=j&&y[i]!=x[j])j=next[j];
  50. 50 i++;j++;
  51. 51 if(j>=m){
  52. 52 ans++;
  53. 53 j=next[j];
  54. 54 }
  55. 55 }
  56. 56 return ans;
  57. 57 }
  58. 58 };
  59. 59
  60. 60 int main()
  61. 61 {
  62. 62 KMP a;
  63. 63 while(a.init())
  64. 64 printf("%d\n",a.kmp_count());
  65. 65 return 0;
  66. 66 }
  67. 67 //如果n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n]。

扩展kmp

  1. 1 #include <algorithm>
  2. 2 #include <iostream>
  3. 3 #include <cstring>
  4. 4 #include <cstdio>
  5. 5
  6. 6 using namespace std;
  7. 7
  8. 8 struct KZ_KMP{//求最长公共前缀
  9. 9 char y[1010];//主串
  10. 10 char x[1010];//模式串
  11. 11 int n,m;
  12. 12 int next[1010];//next[i]:x[i...m-1]与x[0...m-1]的最长公共前缀
  13. 13 int extend[1010];//extend[i]:y[i...n-1]与x[0...m-1]的最长公共前缀
  14. 14
  15. 15 int init(){
  16. 16 scanf("%s%s",y,x);
  17. 17 n=strlen(y);
  18. 18 m=strlen(x);
  19. 19 pre_EKMP();
  20. 20 EKMP();
  21. 21 return 1;
  22. 22 }
  23. 23
  24. 24 void pre_EKMP(){
  25. 25 next[0]=m;
  26. 26 int j=0;
  27. 27 while(j+1<m&&x[j]==x[j+1])j++;
  28. 28 next[1]=j;
  29. 29 int k=1;
  30. 30 for(int i=2;i<m;i++){
  31. 31 int p=next[k]+k-1;
  32. 32 int L=next[i-k];
  33. 33 if(i+L<p+1)next[i]=L;
  34. 34 else{
  35. 35 j=max(0,p-i+1);
  36. 36 while(i+j<m&&x[i+j]==x[j])j++;
  37. 37 next[i]=j;
  38. 38 k=i;
  39. 39 }
  40. 40 }
  41. 41 }
  42. 42
  43. 43 void EKMP(){
  44. 44 int j=0;
  45. 45 while(j<n&&j<m&&x[j]==y[j])j++;
  46. 46 extend[0]=j;
  47. 47 int k=0;
  48. 48 for(int i=1;i<n;i++){
  49. 49 int p=extend[k]+k-1;
  50. 50 int L=next[i-k];
  51. 51 if(i+L<p+1)extend[i]=L;
  52. 52 else{
  53. 53 j=max(0,p-i+1);
  54. 54 while(i+j<n&&j<m&&y[i+j]==x[j])j++;
  55. 55 extend[i]=j;
  56. 56 k=i;
  57. 57 }
  58. 58 }
  59. 59 //printf("%d ",extend[0]);
  60. 60 }
  61. 61 };
  62. 62
  63. 63 int main()
  64. 64 {
  65. 65 KZ_KMP a;
  66. 66 while(a.init()){
  67. 67 }
  68. 68 return 0;
  69. 69 }

kmp与扩展kmp模板的更多相关文章

  1. Manacher模板,kmp,扩展kmp,最小表示法模板

    *N]; //储存临时串 *N];//中间记录 int Manacher(char tmp[]) { int len=strlen(tmp); ; ;i<len;i++) { s[cnt++]= ...

  2. KMP和扩展KMP【转】

    这种东西基本上在纸上自己推导一下就能做出来XD 转发注明出处 KMP 给出两个字符串A(称为模板串)和B(称为子串),长度分别为lenA和lenB,要求在线性时间内,对于每个A[i] (0<=i ...

  3. KMP与扩展KMP

    原文转自:http://www.cppblog.com/MatoNo1/archive/2011/04/17/144390.aspx KMP:给出两个字符串A(称为模板串)和B(称为子串),长度分别为 ...

  4. KMP && Manacher && 扩展KMP整理

    KMP算法: kmp示例代码: void cal_next(char *str, int *next, int len) { next[0] = -1;//next[0]初始化为-1,-1表示不存在相 ...

  5. KMP 、扩展KMP、Manacher算法 总结

    一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...

  6. 666 专题三 KMP &#38; 扩展KMP &#38; Manacher

    KMP: Problem A.Number Sequence d.求子串首次出现在主串中的位置 s. c. #include<iostream> #include<stdio.h&g ...

  7. KMP和扩展KMP

    文章网上太多这里提一下代码细节: KMP: scanf("%s\n",s); scanf("%s\n",t); int ls=strlen(s),lt=strl ...

  8. 【kmp或扩展kmp】HDU 6153 A Secret

    acm.hdu.edu.cn/showproblem.php?pid=6153 [题意] 给定字符串A和B,求B的所有后缀在A中出现次数与其长度的乘积之和 A和B的长度最大为1e6 方法一:扩展kmp ...

  9. KMP 和 扩展KMP

    KMP:在主串S中找子串T的位置KMP算法的时间复杂度O(|S|+|T|). #define maxn 1000 char s[maxn],t[maxn];//s为主串,t为子串 int net[ma ...

随机推荐

  1. 怒转一波,此人整理的Flink特别好

    Apache Flink:特性.概念.组件栈.架构及原理分析 Apache Flink是一个面向分布式数据流处理和批量数据处理的开源计算平台,它能够基于同一个Flink运行时(Flink Runtim ...

  2. C++中的函数重载分析(二)

    1,重载与指针: 1,下面的函数指针将保存哪个函数的地址? int func(int x) { return x; } int func(int a, int b) { return a + b; } ...

  3. 如何通过HTTP API 调取tushare的股票数据

    长久以来,Tushare一直以固定的Python SDK方式为大家提供数据服务. 虽然在基于Python的数据分析和Python的量化策略开发很方便,但习惯用其他语言的同学们表示了“抗议”,于是在Tu ...

  4. jsonp详细原理之一

    /*script标签是不存在跨域请求的,类似的还有img,background:url,link 你可以想象一下,平时的这些标签都是可以直接引入外部资源的,所以是不存在跨域问题的*/ function ...

  5. docker 安装Filebeat

    1.查询镜像 docker search filebeat 2.拉取镜像 我此处选择的是prima/filebeat docker pull prima/filebeat 3.创建配置文件 fileb ...

  6. C51的关键字解释

    参考原文 https://www.cnblogs.com/tianqiang/p/9251486.html [存储种类] 数据类型 [存储器类型] 变量名 [_at_] [地址]: _at_ 地址定位 ...

  7. 基于mybatis拦截器分表实现

    1.拦截器简介 MyBatis提供了一种插件(plugin)的功能,但其实这是拦截器功能.基于这个拦截器我们可以选择在这些被拦截的方法执行前后加上某些逻辑或者在执行这些被拦截的方法时执行自己的逻辑. ...

  8. 关于Linux_系统资源监控_dmesg_free_uptime_uname

    (系统资源查看命令-dmesg[查看系统内核资源信息])->判断服务器的硬件状态 Comment:dmesg | grep CPU,指定查看cpu资源信息 (系统资源查看命令-free[查看内存 ...

  9. 【leetcode】988. Smallest String Starting From Leaf

    题目如下: Given the root of a binary tree, each node has a value from 0 to 25representing the letters 'a ...

  10. 数据中台核心方法论--OneModel为何需要产品化支撑?

    什么是产品化 大部分创业公司都是从一个伟大的想法创意开始的,并且需要有一堆技术专家来实现.我们清楚,伟大的技术并不等同于和伟大的产品,技术可以解决问题,但如果它没有办法法规模化,那这些技术或者能力对用 ...