链接  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. R语言入门视频笔记--4--R的数据输入

    输入 R的数据输入可以大体三种: 1.键盘输出 2.从文本文件导入 3.从Excel中导入数据 一.从键盘输入 首先创建一个数据框,玩玩嘛,瞎建一个 mydata <- data.frame(a ...

  2. android图片上传

    package com.example.center; import java.io.ByteArrayOutputStream;import java.io.InputStream; import ...

  3. LeetCode OJ--Valid Palindrome

    http://oj.leetcode.com/problems/valid-palindrome/ 判断是否为回文串 bool isPalindrome(string s) { ,j = s.leng ...

  4. 虚拟机centos 里tomcat的端口映射到主机 Windows里面

  5. Mac OS X 下安装python的MySQLdb模块

    参考资料: mac os x下python安装MySQLdb模块   http://www.codeif.com/post/1073/ MAC OSX使用Python安装模块有关问题  http:// ...

  6. SPOJ 26108 TRENDGCD - Trending GCD

    Discription Problem statement is simple. Given A and B you need to calculate S(A,B) . Here, f(n)=n, ...

  7. cef network-settings

    Network Settings 目录 1 System network settings 2 Preference service for network settings 3 Command-li ...

  8. android应用开发之View的大小计量单位(px、dpi、dp、dip、sp)

    http://blog.csdn.net/ljianhui/article/details/43601495?ref=myread 一.像素(px)与屏幕分辨率 1)px(Pixels ,像素):对应 ...

  9. jquery+css实现邮箱自动补全

    今天在公司做一个电子商务网站的注册会员时,要求用户在电子邮箱文本框中输入时,给与热点提示常用的电子邮箱,帮助用户选择,提高体验效果.下面是用Jquery+css实现的邮箱自动补全,供大家参考和学习. ...

  10. jmeter.properties控制聚合报告的用户响应时间设置和smmary results

    jmeter.properties的配置Summariser控制输出Summary Results,可以显式rt和tps等信息 Aggregate Report配置可以控制聚合报告的内容,控制90%用 ...