In-depth explanation of string pattern matching algorithms (common algorithms and KMP Algorithms)

Source: Internet
Author: User

String locating is usually called string pattern matching and is one of the most important operations in various processing systems.
The simplest pattern matching algorithm is the backtracking method, that is, the matching of a pattern string and a character in the main string. When the pattern string does not match the main string, the primary string is traced back to the next position starting with the matching of the pattern string. The pattern string is traced back to the first position to continue matching. The time complexity of the algorithm is O (m * n). The algorithm is as follows: Copy codeThe Code is as follows: // simple string pattern matching algorithm. S is the main string, and T is the pattern string. That is, check if S has the same string as T.
Int Index (char * S, char * T, int pos) // Where the pos record starts matching can be directly replaced by 0
{
Int I = pos, j = 0;
While (I <strlen (S) & j <strlen (T) // ensure that the length of the string is not exceeded
{
If (S [I] = T [j])
{++ I; ++ j;} // if the same, continue backward comparison
Else
{I = I-j + 1; j = 0;} // if different, trace back and search again
}
If (j = strlen (T ))
Return I-strlen (T); // If the match is successful, the index at the same starting position as the T string in S is returned.
Else return 0; // If the match fails, 0 is returned.
}

O (m * n) has a relatively high time complexity, so people have discovered the KMP algorithm. The core idea is: when the mismatch occurs, the main string does not backtrack, the pattern string is traced back to the "appropriate" position. The proper position is only related to the pattern string. Therefore, you can first calculate the characters in the pattern string. If the mismatch occurs, which position should be traced back. The overall time complexity O (m + m) of the algorithm ).
The algorithm is as follows:
Copy codeCode: void GetNext (char * T, int * next)
{
Int I = 1, j = 0;
Next [1] = 0;
While (I <strlen (T ))
{
If (j = 0 | T [I] = T [j])
{
++ I; ++ j;
Next [I] = j;
}
Else j = next [j];
}
}
Int KMP (char * S, char * T, int pos)
{
Int I = pos, j = 1;
While (I)
{
If (S [I] = T [j])
{
++ I; ++ j;
}
Else
J = next [j];
}
If (j> strlen (T ))
Return I-T [0];
Else
Return 0;
}

The next operation is not optimal, because he does not consider aaaaaaaaaaaaaaaaaaaaaaab, so there will be a lot of 1 above. The complexity of this algorithm is no different from that of the original SIMPLE algorithm. So let's make a slight change:Copy codeCode: void GetNextEx (char * T, int * next)
{
Int I = 1, j = 0; next [1] = 0;
While (I <strlen (T ))
{
If (j = 0 | T [I] = T [j])
{
++ I; ++ j;
If (T [I] = T [j])
Next [I] = next [j]; // reduce the number of rollbacks
Else next [I] = j; // same as the above algorithm, next [I] = j
}
Else j = next [j];
}
}

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.