KMP algorithm C ++ code

Source: Internet
Author: User
  1. // By hacker
  2. # Include <iostream>
  3. # Include <vector>
  4. # Include <functional>
  5. # Include <algorithm>
  6. Using namespace STD;
  7. Template <typename FTYPE, typename stype>
  8. Struct equal_cmp
  9. {
  10. Bool operator () (const FTYPE & _ F, const stype & _ s) const
  11. {Return _ f = _ s ;}
  12. };
  13. Template <typename randaccessconcept,
  14. Typename protocol CMP>
  15. Inline void
  16. Calfailfunction (
  17. Const randaccessconcept & in,
  18. Int Len,
  19. Vector <int> & ff,
  20. Equalcmp CMP
  21. )
  22. {
  23. Ff. Clear ();
  24. Ff. Reserve (LEN );
  25. Ff. push_back (-1 );
  26. For (INT curr = 1, K =-1; curr <Len; ++ curr)
  27. {
  28. For (; k> = 0 &&! CMP (in [k + 1], in [curr]);
  29. K = FF [k]);
  30. If (CMP (in [k + 1], in [curr]) ++ K;
  31. Ff. push_back (k );
  32. }
  33. }
  34. Template <typename frandaccessconcept,
  35. Typename srandaccessconcept,
  36. Typename protocol CMP>
  37. Inline int
  38. Kmpsearchfirst (
  39. Const frandaccessconcept & D,
  40. Int dlength,
  41. Const srandaccessconcept & P,
  42. Int plength,
  43. Equalcmp CMP,
  44. Int start = 0
  45. )
  46. {
  47. Vector <int> ff;
  48. Calfailfunction (p, plength, FF, CMP );
  49. For (INT I = start, j =-1; I <dlength; ++ I)
  50. {
  51. While (j> = 0 &&! CMP (d [I], p [J + 1]) J = FF [J];
  52. If (CMP (d [I], p [J + 1]) ++ J;
  53. If (j = plength-1) return I-plength + 1;
  54. }
  55. Return-1;
  56. }
  57. Template <typename frandaccessconcept,
  58. Typename srandaccessconcept,
  59. Typename protocol CMP>
  60. Inline int
  61. Kmpsearchall (
  62. Const frandaccessconcept & D,
  63. Int dlength,
  64. Const srandaccessconcept & P,
  65. Int plength,
  66. Vector <int> & result,
  67. Equalcmp CMP
  68. )
  69. {
  70. Vector <int> ff;
  71. Calfailfunction (p, plength, FF, CMP );
  72. Result. Clear ();
  73. For (INT I = 0, j =-1; I <dlength; ++ I)
  74. {
  75. While (j> = 0 &&! CMP (d [I], p [J + 1]) J = FF [J];
  76. If (CMP (d [I], p [J + 1]) ++ J;
  77. If (j = plength-1)
  78. {
  79. Result. push_back (I-plength + 1 );
  80. J = FF [J];
  81. }
  82. }
  83. Return result. Size ();
  84. }
  85. Int main ()
  86. {
  87. Int d [] = {1, 2, 3, 1, 2, 2 };
  88. Char P [] = "/1/2/3/1/2 ";
  89. Vector <int> result;
  90. Int found = kmpsearchall (D, 8, P, 5, result, 1__cmp <int, char> ());
  91. For (INT I = 0; I <found; ++ I)
  92. Cout <result [I] <"";
  93. Cout <Endl;
  94. Return 0;
  95. }

: Updated some codes that can be used directly and used for short mode strings. I recommend a book called flexible string matching.

template<typename ElemType>int MatchSO(ElemType* t, int n, ElemType* p, int m, vector<int>& result){typedef unsigned long long NumberType;const int CharSet = 128;NumberTypeB[CharSet];NumberTypeD = ~0;NumberTypeCheck = 1 << (m-1);NumberTypeBIT = 1;for (int i = 0; i < CharSet; ++i) B[i] = ~0;for (int i = 0; i < m; ++i, BIT <<= 1) B[p[i]] &= ~BIT;result.clear();for (int i = 0; i < n; ++i){D = (D << 1) | B[t[i]];if ((D & Check) == 0) result.push_back(i-m+1);}return result.size();}template<typename ElemType>int MatchSA(ElemType* t, int n, ElemType* p, int m, vector<int>& result){typedef unsigned long long NumberType;const int CharSet = 128;NumberTypeB[CharSet];NumberTypeD = 0;NumberTypeCheck = 1 << (m-1);NumberTypeBIT = 1;for (int i = 0; i < CharSet; ++i) B[i] = 0;for (int i = 0; i < m; ++i, BIT <<= 1) B[p[i]] |= BIT;result.clear();for (int i = 0; i < n; ++i){D = ((D << 1) | 1) & B[t[i]];if ((D & Check) != 0) result.push_back(i-m+1);}return result.size();}template<typename ElemType>int MatchKMP(ElemType* t, int n, ElemType* p, int m, vector<int>& result){int* Prefix = new int[m];{Prefix[0] = -1;for (int i = 1, F = -1; i < m; ++i){while (F >= 0 && p[F+1] != p[i]) F = Prefix[F];if (p[F+1] == p[i]) ++F;Prefix[i] = F;}}{result.clear();for (int i = 0, F = -1; i < n; ++i){while (F >= 0 && p[F+1] != t[i]) F = Prefix[F];if (p[F+1] == t[i]) ++F;if (F+1 == m){result.push_back(i-m+1);F = Prefix[F];}}}delete[] Prefix;return result.size();}

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.