---- KMP string matching algorithm Algorithms

Previous Violence Act mentioned in string matching algorithm, but obviously there is a problem Violence Act: time to move only one character. But in fact, for different match situations, each movement interval can be larger, but each time there is no need to move a:

About KMP algorithm description, we recommend a blog: https://blog.csdn.net/weixin_36604953/article/details/78576637

The blog describes in detail the principles of KMP algorithm. The following code implements the KMP algorithm:

1  // using a brute force method, KMP completed character string matching algorithm Algorithm 
2 # the include " the iostream " 
. 3 #include " String " 
. 4 #include " Vector " 
. 5  the using  namespace STD;
 . 6 Vector < int > & BFmatch ( String &, String &, Vector < int > & );
 . 7 Vector < int > & KMPStrMatch ( String &, String &, Vector < int > & );
 8 voidShowpos (Vector < int > & );
 . 9  int main ()
 10  {
 . 11      String ModelStr, SonStr;
 12 is      Vector < int > POS;
 13 is      COUT << " Please enter a string to be matched: " ;
 14      CIN >> ModelStr;
 15      << COUT endl;
 16      COUT << " Please enter substring: " ;
 . 17      CIN >> SonStr;
 18 is      COUT << endl;
 . 19      // BFmatch (ModelStr, SonStr, POS);
20     KMPStrMatch(ModelStr, SonStr, pos);
21     ShowPos(pos);
22     system("pause");
23 }
24 vector<int>& BFmatch(string & ModelStr, string & SonStr,vector<int>& pos)
25 {
26     for (int i = 0; i < ModelStr.size(); i++)
27     {
28         int k = 0;
29         for (int j = i; k < SonStr.size(); j++, k++)
30         {
31             if (SonStr[k] == ModelStr[j])
32                 continue;
33             else
34                 break;
35         }
36         if (k == SonStr.size())
37             pos.push_back(i);
38     }
39     return pos;
40 }
41 void ShowPos(vector<int>& pos)
42 {
43     if (pos.size() != 0)
44     {
45         cout << "the first position of MatchingStr:";
46         for (int i = 0; i < pos.size(); i++)
47         {
48             cout << pos[i] << "\t";
49         }
50         cout << endl;
51     }
52     else
53         cout << "no such string!" << endl;
54 }
55 vector<int>& KMPStrMatch(string & ModelStr, string & SonStr, vector<int>& pos)
56 {
57     string ComStr;
58     string tmp1, tmp2;
59     int j = 0, i = 0, len = 0;;
60     while(j< (ModelStr.size()- SonStr.size()+1))
61     {
62         if (ModelStr[j] != SonStr[0])
63         {
64             j++;
65             continue;//首位不匹配直接加1
66          }
 67          the else 
68          {
 69              the while ((J <ModelStr.size ()) && (ModelStr [J] == SonStr [I])) // && foregoing constraint ensures that memory does not occur bounds
 70              {
 71 is                  J ++ ;
 72                  I ++ ;
 73 is              }
 74              IF (I == SonStr.size ())
 75                  pos.push_back (J - SonStr.size ());
 76              J = J - I;
 77              ComStr SonStr.substr = ( 0 , I - . 1 );
 78              for (int q = 1; q < ComStr.size(); q++)
79             {
80                 tmp1=ComStr.substr(q, ComStr.size() - 1);
81                 tmp2=ComStr.substr(0, ComStr.size() - 1 - q);
82                 if (tmp1 == tmp2)
83                     len++;
84             }
85             j = j + i-len;
86             i = 0;
87             len = 0;
88         }
89      }
 90      return postage;
91 }

In short, KMP's core idea is: to decide to move the length of the string to be matched by the length of the matching portion of the string, rather than just moving each one.

 

Guess you like

Origin www.cnblogs.com/shaonianpi/p/11413551.html