作用

  • KMP主要应用在字符串匹配上。

思想

  • KMP的主要思想是当出现字符串不匹配时,可以知道一部分之前已经匹配的文本内容,可以利用这些信息避免从头再去做匹配了。
  • 如何记录已经匹配的文本内容,是KMP的重点,也是next数组肩负的重任。

前缀表

  • 前缀表是用来回退的,它记录了模式串与主串(文本串)不匹配的时候,模式串应该从哪里开始重新匹配。

  • 注意:

    • 如果前缀表中下标不统一减一,则回跳时需看上一个位置的记录
    • 如果前缀表整体右移一位,且0位置初始化为-1,则回跳时就看本位置的记录

next数组的实现:1.初始化;2.前后缀不相同;3.前后缀相同;next

  1. // j指向前缀末尾位置,同时表示i及其之前的最长前缀的长度; i指向后缀末尾位置
  2. // *******************************************************************
  3. // 初始化
  4. next[0] = -1; j = -1;
  5. // *******************************************************************
  6. // 前后缀不相同
  7. while (j >= 0 && s[i] != s[j + 1]) {
  8. j = next[j]; // 向前回退
  9. }
  10. // *******************************************************************
  11. // 找到相同的前后缀
  12. if (s[i] == s[j + 1]) {
  13. j++;
  14. }
  15. next[i] = j;
  16. // *******************************************************************
  17. // 整体代码
  18. void getNext(int* next, const string& s){
  19. int j = -1;
  20. next[0] = j;
  21. for(int i = 1; i < s.size(); i++) { // 注意i从1开始
  22. while (j >= 0 && s[i] != s[j + 1]) { // 前后缀不相同了
  23. j = next[j]; // 向前回退
  24. }
  25. if (s[i] == s[j + 1]) { // 找到相同的前后缀
  26. j++;
  27. }
  28. next[i] = j; // 将j(前缀的长度)赋给next[i]
  29. }
  30. }

建立next数组

KMP精讲1.gif


KMP精讲2.gif


整体减一后的next数组

KMP精讲4.gif


例题:28. 实现 strStr()

  1. // 前缀表统一减一
  2. class Solution {
  3. public:
  4. void getNext(int* next, const string& s) {
  5. int j = -1;
  6. next[0] = j;
  7. for(int i = 1; i < s.size(); i++) { // 注意i从1开始
  8. while (j >= 0 && s[i] != s[j + 1]) { // 前后缀不相同了
  9. j = next[j]; // 向前回退
  10. }
  11. if (s[i] == s[j + 1]) { // 找到相同的前后缀
  12. j++;
  13. }
  14. next[i] = j; // 将j(前缀的长度)赋给next[i]
  15. }
  16. }
  17. int strStr(string haystack, string needle) {
  18. if (needle.size() == 0) {
  19. return 0;
  20. }
  21. int next[needle.size()];
  22. getNext(next, needle);
  23. int j = -1; // // 因为next数组里记录的起始位置为-1
  24. for (int i = 0; i < haystack.size(); i++) { // 注意i就从0开始
  25. while(j >= 0 && haystack[i] != needle[j + 1]) { // 不匹配
  26. j = next[j]; // j 寻找之前匹配的位置
  27. }
  28. if (haystack[i] == needle[j + 1]) { // 匹配,j和i同时向后移动
  29. j++; // i的增加在for循环里
  30. }
  31. if (j == (needle.size() - 1) ) { // 文本串s里出现了模式串t
  32. return (i - needle.size() + 1);
  33. }
  34. }
  35. return -1;
  36. }
  37. };
  1. // 前缀表(不减一)
  2. class Solution {
  3. public:
  4. void getNext(int* next, const string& s) {
  5. int j = 0;
  6. next[0] = 0;
  7. for(int i = 1; i < s.size(); i++) {
  8. while (j > 0 && s[i] != s[j]) {
  9. j = next[j - 1];
  10. }
  11. if (s[i] == s[j]) {
  12. j++;
  13. }
  14. next[i] = j;
  15. }
  16. }
  17. int strStr(string haystack, string needle) {
  18. if (needle.size() == 0) {
  19. return 0;
  20. }
  21. int next[needle.size()];
  22. getNext(next, needle);
  23. int j = 0;
  24. for (int i = 0; i < haystack.size(); i++) {
  25. while(j > 0 && haystack[i] != needle[j]) {
  26. j = next[j - 1];
  27. }
  28. if (haystack[i] == needle[j]) {
  29. j++;
  30. }
  31. if (j == needle.size() ) {
  32. return (i - needle.size() + 1);
  33. }
  34. }
  35. return -1;
  36. }
  37. };