链接  HDU 1711 Number Sequence

KMP 算法

我以自己理解写的,写的不对,不明白的地方海王子出来,一起共同学习;

字符串匹配 就是KMP,一般思想,用一个for循环找开头   如果开头一样进入第二个for循环;这样统计  直观 容易理解。但是时间复杂度比较长。所以就有KMP  这个算法了

KMP 大体思想因为在上一个方法之中,有的字符重复比较了,使得找到目标字符窜效率比较低,

比如   :目标串为:   ① a b a b c

在                 ② a b a a b a b c d a b c d b c  这个字符窜中找到上述目标串;

如用  i,j  来表示①,②两个字符串的下标,假设下标从一开始;

i=1,j=1    a=a;

i=2,j=2    b=b;

i=3,j=3    a=a;

i=4;j=4    b!=a;  此时如果只将①字符串右移动一位,虽然可以;但是,假如你移动两位看看怎么样?

a b a b c

a b a a b a b c d a b c d b c

是不是觉得移动2格对得非常整齐?

那么为什么移动两个?

第3个a与第一个a正好重复!所以我们知道重复单元就知道移动多少。需要一个数组记录当两个字符不相等,i 应该指向哪个!记录重复单元的值,叫做部分匹配值!不懂可以百度部分匹配值!以上就是KMP 核心思想

下面贴一个代码!!两种写法!//屏蔽是另一种写法

[cpp] view
plain
 copy

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<algorithm>
  5. using namespace std;
  6. int N[1000005],M[10005],a[10005],n,m;
  7. void set_a()
  8. {
  9. int i,j;
  10. a[1]=0;
  11. j=1;
  12. for(i=2; i<=m; i++)
  13. {
  14. if(M[j]==M[i])
  15. {
  16. a[i]=j;
  17. j++;
  18. }
  19. else if(M[j]!=M[i]&&M[i]==M[1])
  20. {
  21. a[i]=1;
  22. j=2;
  23. }
  24. else
  25. {
  26. a[i]=0;
  27. j=1;
  28. }
  29. }
  30. //    for(i=1; i<=m; i++)
  31. //        printf("%d ",a[i]);
  32. //    printf("\n");
  33. }
  34. int kmp()
  35. {
  36. int i=1,j=1,k;
  37. set_a();
  38. while(i<=n)
  39. {
  40. if(j==1&&N[i]!=M[j]) i++;
  41. if(N[i]==M[j])
  42. {
  43. i++;
  44. j++;
  45. }
  46. else j=a[j-1]+1;
  47. if(j==m+1) return i-m;
  48. }
  49. return -1;
  50. }
  51. int main()
  52. {
  53. int t;
  54. scanf("%d",&t);
  55. while(t--)
  56. {
  57. //int b; scanf("%d",&b);
  58. int i;
  59. scanf("%d%d",&n,&m);
  60. memset(a,0,sizeof(a));
  61. for(i=1; i<=n; i++)
  62. scanf("%d",&N[i]);
  63. for(i=1; i<=m; i++)
  64. scanf("%d",&M[i]);
  65. printf("%d\n",kmp());
  66. }
  67. return 0;
  68. }
  69. ///**********************KMP****************************/
  70. //
  71. //#include<stdio.h>
  72. //#include<string.h>
  73. //using namespace std;
  74. //int lena,lenb;
  75. //int a[1000005],b[10005];
  76. //int next[200000];
  77. //void set_naxt()//子串的next数组
  78. //{
  79. //    int i=0,j=-1;
  80. //    next[0]=-1;
  81. //    while(i<lenb)
  82. //    {
  83. //        if(j==-1||b[i]==b[j])
  84. //        {
  85. //            i++;
  86. //            j++;
  87. //            next[i]=j;
  88. //        }
  89. //        else
  90. //            j=next[j];
  91. //    }
  92. //    // printf(">>%d\n",next[lenb-1]);
  93. //    for(i=1; i<=lenb; i++)
  94. //        printf("%d ",next[i]);
  95. //    printf("\n");
  96. //}
  97. //
  98. //int kmp()
  99. //{
  100. //    int i=0,j=0;
  101. //    set_naxt();
  102. //    while(i<lena)
  103. //    {
  104. //        if(j==-1||a[i]==b[j])
  105. //        {
  106. //            i++;
  107. //            j++;
  108. //        }
  109. //        else
  110. //            j=next[j];
  111. //        if(j==lenb)
  112. //            return i-j+1;
  113. //    }
  114. //    return -1;
  115. //}
  116. //int main()
  117. //{
  118. //    int i,t;
  119. //    scanf("%d",&t);
  120. //    while(t--)
  121. //    {
  122. //        memset(next,0,sizeof(next));
  123. //        scanf("%d%d",&lena,&lenb);
  124. //        for(i=0; i<lena; i++)
  125. //            scanf("%d",&a[i]);
  126. //        for(i=0; i<lenb; i++)
  127. //            scanf("%d",&b[i]);
  128. //        printf("%d\n",kmp());
  129. //    }
  130. //}

HDU 1711(KMP)字符串匹配的更多相关文章

  1. {Reship}{KMP字符串匹配}

    关于KMP字符串匹配的介绍和归纳,作者的思路非常清晰,推荐看一下 http://blog.csdn.net/v_july_v/article/details/7041827

  2. 洛谷P3375 - 【模板】KMP字符串匹配

    原题链接 Description 模板题啦~ Code //[模板]KMP字符串匹配 #include <cstdio> #include <cstring> int cons ...

  3. Luogu 3375 【模板】KMP字符串匹配(KMP算法)

    Luogu 3375 [模板]KMP字符串匹配(KMP算法) Description 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来 ...

  4. 洛谷P3375 [模板]KMP字符串匹配

    To 洛谷.3375 KMP字符串匹配 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果 ...

  5. P3375 【模板】KMP字符串匹配

    P3375 [模板]KMP字符串匹配 https://www.luogu.org/problemnew/show/P3375 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在 ...

  6. 洛谷—— P3375 【模板】KMP字符串匹配

    P3375 [模板]KMP字符串匹配 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next. (如 ...

  7. P3375 模板 KMP字符串匹配

    P3375 [模板]KMP字符串匹配 来一道模板题,直接上代码. #include <bits/stdc++.h> using namespace std; typedef long lo ...

  8. KMP字符串匹配 模板 洛谷 P3375

    KMP字符串匹配 模板 洛谷 P3375 题意 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.(如果 ...

  9. KMP字符串匹配学习

    KMP字符串匹配学习 牛逼啊 SYC大佬的博客

  10. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

随机推荐

  1. Arduino学习笔记1---开发环境搭建

    主要内容:(一). Arduino IDE的下载及安装 (二). Arduino IDE的应用 (三). Arduino的程序结构 (四). Arduino程序的编译及下载 (一). Arduino ...

  2. 树莓派用gobot测试舵机的使用

    package main import ( "gobot.io/x/gobot" "gobot.io/x/gobot/drivers/gpio" "g ...

  3. linux svn配置hooks

    先创建仓库: svnadmin create /data/svn/my.com 再配置权限: #cd /data/svn/my.com/conf/ #vim svnserve.conf 配置 [gen ...

  4. CentOS 5.4 final下Systemtap的安装

    CentOS 5.4 final下Systemtap的安装  时间:2015-02-11来源:linux网站 作者:zklth  一.Systemtap运行环境需求   (1)linux kernel ...

  5. 管理weblogic服务的启动和停止

    2012-11-10 12:58 26036人阅读 评论(4) 收藏 举报 分类: WebLogic(10) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 介绍 Weblog ...

  6. python异常捕获异常堆栈输出

    python异常捕获异常堆栈输出 学习了:https://blog.csdn.net/chris_grass/article/details/77927902 import traceback def ...

  7. IntelliJ IDEA cannot resolved 处理

    IntelliJ IDEA cannot resolved 处理 学习了:https://stackoverflow.com/questions/21577573/intellij-idea-can- ...

  8. 使用Python写的第一个网络爬虫程序

    今天尝试使用python写一个网络爬虫代码,主要是想訪问某个站点,从中选取感兴趣的信息,并将信息依照一定的格式保存早Excel中. 此代码中主要使用到了python的以下几个功能,因为对python不 ...

  9. v-if v-else-if v-else

    1.代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  10. UFLDL教程笔记及练习答案三(Softmax回归与自我学习***)

    :softmax回归 当p(y|x,theta)满足多项式分布,通过GLM对其进行建模就能得到htheta(x)关于theta的函数,将其称为softmax回归. 教程中已经给了cost及gradie ...