Algorithm: KMP, algorithm kmp

Source: Internet
Author: User

Algorithm: KMP, algorithm kmp

The KMP algorithm is an algorithm used to search for substrings in the target string.


Blog purpose: Use graphs to speak.


Basic Idea of KMP Algorithm

(1) obtain the next [j] value for each character in the mode string;
(2) perform mode matching.
Assume that I and j are the current positions of the characters in the comparison between the main string and the mode string, and assign the initial values of I and j to 0. During the matching process, if si = tj, I and j increase by 1 and continue the comparison. Otherwise, I remains unchanged, j returns to next [j] for a new round of comparison. This recurrence continues until the following two situations occur:
When j returns a value of next [j], the matching is successful, and I and j increase by 1 to continue matching;
When j is returned to a value of 0, that is, next [j] = 0, it indicates that the current character matching of the Main string fails. At this time, the main string is slide to the right, that is, starting a new round of matching from I + 1, j = 0.

KMP Matching Algorithm

If you don't understand it, you just need to attach the code three or four times.
If you don't understand anything, ask, but try to study it on your own.
# Include <iostream. h>
# Include <string. h>
# Include <stdlib. h>
Const int maxLen = 128;
Class String
{
Int curLen; // The current length of the string
Char * ch; // string storage array
Public:
String (const String & ob );
String (const char * init );
String ();
~ String ()
{
Delete [] ch;
}
Int Length () const
{
Return curLen;
}
String * operator () (int pos, int len );
Int operator = (const String & ob) const
{
Return strcmp (ch, ob. ch) = 0;
}
Int operator! = (Const String & ob) const
{
Return strcmp (ch, ob. ch )! = 0;
}
Int operator! () Const
{
Return curLen = 0;
}
String & operator = (const String & ob );
String & operator ++ = (const String & ob );
Char & operator [] (int I );
Int fastFind (String pat) const;
// Void fail (const char * T, int * & f );
Void fail (int * & f );
};
String: String (const String & ob) // copy constructor: copy from existing String ob
{
Ch = new char [maxLen + 1];
If (! Ch)
{
Cout <"Allocation Error \ n ";
Exit (1 );
}
CurLen = ob. curLen;
Strcpy (ch, ob. ch );
}
String: String (const char * init) // copy constructor: copy from the existing character array * init
{
Ch = new char [maxLen + 1];
If (! Ch)
{
Cout <"Allocation Error \ n ";
Exit (1 );
}
CurLen = strlen (init );
Strcpy (ch, init );
}
String: String () // constructor: creates an empty String.
{... Remaining full text>

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.