C # KMP Algorithm

Source: Internet
Author: User
Tags rounds

/// <Summary>
/// Evaluate the Backtracking function of a string.
/// The subscript of the agreed sequence starts from 0.
/// The Backtracking function is the ing from the Integer Set [0, n-1] to N. N is the length of the string.
/// Definition of the backtracking function:
/// Set non-empty sequence L, and I as its legal subscript;
// L [I] pre-sequence set is: {empty set, all the sub-sequences in L with the I-1 as the final element}
/// L's pre-sequence set is: {empty set, and all sub-sequences in L with 0 as the first element are}
/// The Backtracking function value of subscript I is defined:
/// If I = 0, the Backtracking function value is-1
/// Otherwise, the Backtracking function value of I is the maximum length of equal elements in the pre-sequence set of I and the pre-sequence set of L, but the two equal elements cannot be the same substring in L, for example, [0-i, 1] ~ [I-1, 0] reversed
/// In other words, set the set V = {X, X to the pre-sequence set of I, and X to the pre-sequence set of L, and the length of X is less than I }, the backtracking function value is max (V ). length
/// When I = 0, there is no such X, so it is agreed that the Backtracking function value is-1.
/// Meaning of the Backtracking function:
/// If the character of the substring that is marked as J is not matched with the primary string, the substring is traced back to next [J] to continue matching with the primary string. If next [J] =-1, then, the matching point of the primary string is moved one by one, starting with matching the first element of the Child string.
/// Match the same general pattern Algorithm In comparison, KMP skips several rounds of matching in case of mismatch through the Backtracking function (the sliding distance to the right may be greater than 1)
/// The KMP algorithm ensures that the matching of the previous rounds is not matched. This proves that
/// </Summary>
/// <Param name = "pattern"> the pattern string, which is called a substring in the preceding comment </param>
/// <Returns> the Backtracking function is the core of the KMP algorithm. This function obtains the Backtracking function according to its definition, and the KMP function uses the Backtracking function according to its meaning. </Returns>
Public static int [] Next (string pattern)
{
Int [] Next = new int [pattern. Length];
Next [0] =-1;
If (pattern. Length <2) // It is more efficient if only one element does not use KMP.
{
Return next;
}

Next [1] = 0; // The backtracking function value of the second element must be 0, which proves that:
// The pre-sequence set of 1 is {empty set, L [0]}, and the length of L [0] is not less than 1, so it is eliminated. The length of the empty set is 0, therefore, the value of the Backtracking function is 0.
Int I = 2; // the index of the character whose next value is being calculated
Int J = 0; // The intermediate variable required to calculate the next value. At the beginning of each iteration, J is always next [I-1].
While (I <pattern. Length) // it is obvious that when I = pattern. length, the next value of all characters has been calculated and the task has been completed.
{// Status
If (pattern [I-1] = pattern [J]) // you must first remember that in the implementation of this function, the next value for iterative calculation starts from the third element.
{// If l [I-1] is equal to L [J], next [I] = J + 1
Next [I ++] = ++ J;
}
Else
{// If not equal, check the next possible value of next [I] ---- next [J]
J = next [J];
If (j =-1) // If J =-1, the value of next [I] is 1.
{// This part can be extracted and combined with the outer judgment
// The KMP in the bookCode A difficult reason to understand is that it has been optimized to obscure its actual logic.
Next [I ++] = ++ J;
}
}
}
Return next;
}


/// <Summary>
/// The difference between the KMP function and the normal pattern matching function is that the next function is used to move the pattern string multiple digits to the right at a time.
/// The essence of the next function is to extract repeated computations.
/// </Summary>
/// <Param name = "Source"> main string </param>
/// <Param name = "pattern"> used to find the mode string at a position in the master string </param>
/// <Returns>-1 indicates no matching; otherwise, a matched label is returned. </returns>
Public static int executekmp (string source, string pattern)
{
Int [] Next = next (pattern );
Int I = 0; // master string pointer
Int J = 0; // mode string pointer
// If the child string does not match and the master string does not search
While (j <pattern. Length & I <source. length)
{
If (source [I] = pattern [J]) // The logical meanings of I and j are embodied in this, used to indicate whether the primary and mode strings are equal in this iteration
{
I ++;
J ++;
}
Else
{
J = next [J]; // iterative Backtracking Based on the indication
If (j =-1) // There is a situation with backtracing. This is the second case.
{
I ++;
J ++;
}
}
}
// If J = pattern. length, it indicates that the loop exit is because the child string has been matched but not the master string has been exhausted.
Return j <pattern. length? -1: I-J;
}

PS: I personally think that the KMP algorithm is a very difficult algorithm, proving that it requires two pages. However, understanding and proof are not the same thing.

Related Article

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.