KMP算法

素颜马尾好姑娘i 2021-09-11 02:08 376阅读 0赞

时间紧迫直接贴代码,后期有时间再补上

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <ostream>
  5. #include <iterator>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <map>
  9. #include <queue>
  10. #include <unordered_set>
  11. #include "cstring"
  12. #define N 1001
  13. using namespace std;
  14. typedef struct MyString {
  15. char *ch;
  16. int length;
  17. } MyString;
  18. int getStringLength(const char array[]) {
  19. int length = 0;
  20. if (array == nullptr) {
  21. return length;
  22. }
  23. while (array[length] != '\0') {
  24. length++;
  25. }
  26. return length;
  27. }
  28. /** * 获取next数组的内容 * @param substr 模式串 * @param next next数组 */
  29. void getNext(MyString substr, int next[]) {
  30. int j = 1;
  31. int t = 0;
  32. next[1] = 0;
  33. while (j < substr.length) {
  34. if (t == 0 || substr.ch[j] == substr.ch[t]) {
  35. next[j + 1] = t + 1;
  36. t++;
  37. j++;
  38. } else {
  39. t = next[t];
  40. }
  41. }
  42. }
  43. /* P 为模式串,下标从 0 开始 */
  44. void GetNext(string P, int next[]) {
  45. int p_len = P.size();
  46. int i = 0; // P 的下标
  47. int j = -1;
  48. next[0] = -1;
  49. while (i < p_len - 1) {
  50. if (j == -1 || P[i] == P[j]) {
  51. i++;
  52. j++;
  53. next[i] = j;
  54. } else
  55. j = next[j];
  56. }
  57. }
  58. int KMP(MyString str, MyString substr, const int next[]) {
  59. int i = 1;
  60. int j = 1;
  61. while (i <= str.length && j <= substr.length) {
  62. if (j == 0 || str.ch[i] == substr.ch[j]) {
  63. i++;
  64. j++;
  65. } else {
  66. j = next[j];
  67. }
  68. }
  69. if (j > substr.length) {
  70. return (i - substr.length);
  71. } else {
  72. return 0;
  73. }
  74. }
  75. int main() {
  76. MyString str, substr;
  77. str.ch = " abcabaaabaabcac";
  78. substr.ch = " abaabcac";
  79. // substr.ch = " aabbcabbaa";
  80. str.length = getStringLength(str.ch);
  81. substr.length = getStringLength(substr.ch);
  82. int *next = static_cast<int *>(malloc((substr.length) * sizeof(int)));
  83. getNext(substr, next);
  84. for (int i = 1; i < substr.length; i++) {
  85. cout << next[i] << " ";
  86. }
  87. cout << endl;
  88. cout << str.length << " " << substr.length << endl;
  89. int kmp = KMP(str, substr, next);
  90. cout << kmp << endl;
  91. return 0;
  92. }

发表评论

表情:
评论列表 (有 0 条评论,376人围观)

还没有评论,来说两句吧...

相关阅读

    相关 KMP算法

    1.原始的字符串匹配方法 算法基本思想:从主串S的第pos个字符起和模式的第一个字符比较之,若相等,则继续逐个比较后续字符;否则从主串的下一个字符起再重新和模式的字符比较

    相关 KMP算法

    kmp算法又称“看毛片”算法,是一个效率非常高的字符串匹配算法。不过由于其难以理解,所以在很长的一段时间内一直没有搞懂。虽然网上有很多资料,但是鲜见好的博客能简单明了地将其讲清

    相关 KMP算法

    1. KMP算法 1.1 定义     Knuth-Morris-Pratt 字符串查找算法,简称为 “KMP算法”,常用于在一个文本串S内查找一个模式串P 的出现

    相关 KMP算法

    核心:1.当遇到不可匹配的字符的时候,我们希望找到一些规律,可以将搜索串往后多滑动几位    2.构建《部分匹配表》 下面链接是阮大神的见解,写的非常棒!    [字符串

    相关 Kmp算法

    举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD"? ![bg2013050101.jpg][]

    相关 KMP算法

    KMP算法的思想是 主串S 和模式串W,判断W是否匹配S 如果主串S在i位置与W串在j位置出现不匹配的情况的时候,利用已经得到的匹配把W串尽量右移动一段距离。 用伪代码写

    相关 KMP算法

    一 KMP算法介绍 1 KMP是一个解决模式串在文本串是否出现过,如果出现过,求算出现的位置的经典算法。 2 Knuth-Morris-Pratt 字符串查找算法,简称