KMP algorithm Learning

Source: Internet
Author: User

1. What is KMP algorithm?

The knuth-Morris-Pratt string search algorithm (often referred to as the "KMP algorithm") is used in a "main text string"SSearch for a "word" in"WTo avoid re-checking the previously matched characters. (Match the pattern string in the original string)


II,KMP demo


Http://staff.ustc.edu.cn /~ Ypb/jpk/ Flash/find_kmp.swf


Iii. KMP principles

KMP is the most common improved algorithm. It can effectively jump a few characters to the backend when the matching process is not matched, thus speeding up the matching.


In the KMP algorithm, there is an array called a prefix array, and some are called. Each pattern string has a fixed next array. Of course, it also describes the degree of symmetry of the substring, the higher the degree, the larger the value. Of course, the chance of re-matching may be greater before.


For an understanding of the next array, see http://blog.csdn.net/yearn520/article/details/6729426#t0




 

[CPP]View plaincopy
  1. Void setprefix (const char * pattern, int prefix [])
  2. {
  3. Int I;
  4. Int Len = strlen (pattern); // The length of the pattern string.
  5. Prefix [0] = 0;
  6. For (I = 1; I <Len; I ++)
  7. {
  8. Int K = prefix [I-1];
  9. // Recursively determine whether sub-symmetry exists. If K is set to 0, sub-symmetry is no longer available. Pattern [I]! = Pattern [k] indicates that although symmetric, the value after symmetric is not equal to the current character value, so the recurrence is continued.
  10. While (pattern [I]! = Pattern [k] & K! = 0) // when I is equal to 14, evaluate the value of prefix [14]
  11. K = prefix [k-1]; // continue Recursion
  12. If (pattern [I] = pattern [k]) // finds the sub-symmetry, or directly inherits the symmetry above, both of which are based on ++
  13. Prefix [I] = k + 1;
  14. Else
  15. Prefix [I] = 0; // if all the sub-symmetry is traversed, this new character is not symmetric.
  16. }
  17. Prefix [0] =-1;
  18. }

Iv. test example [CPP]View plaincopy
  1. # Include <iostream>
  2. # Include <cstring>
  3. # Include <string>
  4. Using namespace STD;
  5. String SORG;
  6. String spat;
  7. Int prefix [10000];
  8. Int result [20];
  9. Void Init ()
  10. {
  11. SORG = "";
  12. Spat = "";
  13. Memset (prefix, 0, sizeof (INT) * 10000 );
  14. // Memset (result, 0, sizeof (INT) * 20 );
  15. }
  16. Void setprefix (string temp, int next [])
  17. {
  18. Int Len = temp. Size ();
  19. Next [0] = 0;
  20. For (INT I = 1; I <Len; I ++)
  21. {
  22. Int K = next [A I-1];
  23. While (temp [k]! = Temp [I] & K! = 0)
  24. K = next [k-1];
  25. If (temp [k] = temp [I])
  26. Next [I] = k + 1;
  27. Else
  28. Next [I] = 0;
  29. }
  30. Next [0] =-1;
  31. For (INT I = 0; I <Len; I ++)
  32. If (next [I]> = 1)
  33. Next [I] = next [I]-1;
  34. }
  35. Int KMP (string S1, string S2)
  36. {
  37. Int number = 0;
  38. Int I = 0;
  39. Int J = 0;
  40. While (I <(INT) s1.size () & J <(INT) s2.size ())
  41. {
  42. If (j =-1 | S1 [I] = S2 [J])
  43. {
  44. I ++;
  45. J ++;
  46. }
  47. Else
  48. J = prefix [J];
  49. If (j = s2.size ())
  50. {
  51. I = I-j + 1;
  52. J = 0;
  53. Number ++;
  54. }
  55. }
  56. Return number;
  57. }
  58. Int main ()
  59. {
  60. Int T;
  61. Cin> T;
  62. Memset (result, 0, sizeof (INT) * 20 );
  63. For (INT I = 0; I <t; I ++)
  64. {
  65. Init ();
  66. // Cout <"input spat string:" <Endl;
  67. Cin> spat;
  68. // Cout <"input SORG string:" <Endl;
  69. Cin> SORG;
  70. Setprefix (spat, prefix );
  71. Result [I] = KMP (SORG, spat );
  72. }
  73. For (Int J = 0; j <t; j ++)
  74. Cout <result [J] <Endl;
  75. Return 0;
  76. }




KMP algorithm Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.