String Matching Algorithm KMP Algorithm

Source: Internet
Author: User

When talking about the string matching algorithm in the data structure, we mention the Simple Matching Algorithm and the KMP matching algorithm.

The simple matching algorithm is a simple match character. If any character does not match, it iterates the next position in the source string for matching, in this way, there will be a lot of non-conforming matching redundant comparisons. Assume that the source string is n long and the string is M. The worst time complexity of the algorithm is M * (N-m + 1), which is recorded as O (N * m ); here we will not explain much about the simple matching algorithm.

KMP algorithm:

The KMP algorithm does not start from the source string. For example, if you want to match a string (abaabcac) in the source string (acabaabcacaabc), you can start from the string abaabcac, when analyzing strings, an array is used to store strings with the same length as the header string and the tail string. For example, abaabcac,

A next [0] =-1, specifying the next value corresponding to the first character as-1;

AB next [1] = 0, because for character B, the front side of string a does not have the header string and the tail string symmetric, so it is 0;

ABA next [2] = 0; because there is no symmetric header string and tail string for the sub-string AB, It is 0;

Abaa next [3] = 1; because for the sub-string ABA, there is symmetric equality between the first string a and the end string a, and its length is 1, so it is 1;

Abaab next [4] = 1; because for the substring abaa, there is symmetric equality between the substring A and the substring A, and its length is 1, so it is 1;

Abaabc next [5] = 2; because for the substring abaab, there is symmetric equality between the substring AB and the substring AB, and its length is 2, so it is 2;

Abaabca next [5] = 0; because there is no header string or tail string symmetric for the sub-string abaab, It is 0;

Abaabcac next [6] = 1; because for the substring abaabca, there is symmetric equality between the substring A and the substring A, and its length is 1, so it is 1;

Summary:

  

J            0    1    2    3    4    5    6    7P            a    b    a    a    b    c    a    cnext(j)      -1    0    0    1    1    2    0    1

The code for getting the next array is as follows:

// Obtain the next array void getnext (char * STR, char * Next) {Int J = 0; int K =-1; int length = strlen (STR ); next [0] =-1; while (j <length) {If (k =-1 | STR [J] = STR [k]) {J ++; k ++; next [J] = K;} else K = next [k] ;}}

Then, during the matching process, if a non-matching phenomenon is encountered, the next [I] value indicates that there are n headers and the tail substrings are equal, that is, search for matching directly from the value of next [I] As the subscript. The complexity is O (m + n) KMP implementation code:

// SRC is the string to be matched. Pat is the string model int KMP (char * SRC, char * Pat) {char next [100]; getnext (Pat, next ); int lengthp = strlen (PAT); int lengths = strlen (SRC); int poss = 0, posp =-1; bool flag = false; while (poss <lengths & posp <lengthp) {If (posp =-1 | SRC [poss] = Pat [posp]) {If (FLAG) poss ++; posp ++;} else {posp = next [posp]; flag = true ;}} if (posp <lengthp) Return-1; else return poss-lengthp ;}

 

Complete code:

 

# Include <stdio. h> # include <string. h> // obtain the next array void getnext (char * STR, char * Next) {Int J = 0; int K =-1; int length = strlen (STR); next [0] =-1; while (j <length) {If (k =-1 | STR [J] = STR [k]) {J ++; k ++; next [J] = K ;} else K = next [k] ;}// SRC is the string to be matched, and Pat is the string model int KMP (char * SRC, char * Pat) {char next [100]; getnext (Pat, next); int lengthp = strlen (PAT); int lengths = strlen (SRC); int poss = 0, posp =-1; bool flag = false; while (poss <lengths & posp <lengthp) {If (posp =-1 | SRC [poss] = Pat [posp]) {If (FLAG) poss ++; posp ++;} else {posp = next [posp]; flag = true ;}} if (posp <lengthp) Return-1; else return poss-lengthp;} int main () {char SRC [100]; char Pat [50]; printf ("Enter the string to match and the string template (string ): \ n "); scanf (" % S % s ", SRC, Pat); int F = KMP (SRC, Pat ); printf ("the subscript of the matched position in the meta string is % d", f); Return 0 ;}

 

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.