String matching algorithm

Source: Internet
Author: User

String match is a problem that is often encountered in real-world engineering, where the input is composed of the original string (string) and the substring (also known as the pattern), and the output is the first occurrence of the substring in the original string. Usually the exact string search algorithm includes brute force search (Brute), KMP. The following analysis of these methods and give their implementation. Suppose the original string length m, the string length is N.

1. Brute Force.

This method, also known as violent search, is also the most easily thought-out method.

Pretreatment time O (0)

Match time Complexity O (n*m)

Main process: Starting from the original string search, if there is no match, then the original search location +1 continue.

/*ret=0,find does not match the text *ret =-1, the input does not meet the requirements *ret = the length of the -2,find is less than the length of the text * If matched, returns the position where the match begins in text */int brute_force (const char *text, const char *find) {    int ret = 0;    if (text = = NULL | | find = NULL)    {        ret =-1;        printf ("Func brute_force () err:%d\n", ret);        return ret;    }    int find_len = strlen (find);    int Text_len = strlen (text);    if (Text_len < Find_len)    {        ret =-2;        printf ("Func brute_force () err:%d\n", ret);        return ret;    }    char *psmall = find;    char *pbig = text;    while (*pbig! = ')    } {        if (*pbig = = *psmall)        {            pbig++;            psmall++;        }        else        {            psmall = find;            pbig++;        }        if (*psmall = = ' + ')        {            ret = (pbig-text)-(psmall-find);            break;        }    }    return ret;}

2. KMP.

KMP is a classic string matching algorithm.

Pretreatment time: O (M)

Match time complexity: O (N)

Main process: By preprocessing the string, when the discovery cannot match, can not carry on the backtracking.

int KMP (const char *text, const char *find) {//judgment input int ret = 0;        if (text = = NULL | | find = NULL) {ret =-1;        printf ("Func brute_force () err:%d\n", ret);    return ret;    }//Determine the length of the matched string int find_len = strlen (find);    int Text_len = strlen (text);        if (Text_len < Find_len) {ret =-2;        printf ("Func brute_force () err:%d\n", ret);    return ret;    }//Set up a matching array int next[find_len];    memset (Next, 0, find_len*sizeof (int));    Next[0] = 0;    NEXT[1] = 0;    int j = 0;    int i = 1;            while (I < Find_len) {if (j==0 | | find[i] = = Find[j]) {i++;            j + +;            if (find[i]! = Find[j]) {next[i] = j;            } else {Next[i] = next[j];        }} else {j = next[j];    }} i=0;    j=0;   while (i <=text_len && j<=find_len) {if (Text[i]==find[j])     {i++;        j + +;            } else {j = next[j];            if (j = = 0) {i++;        }} if (J==find_len) {ret = i-j; }} return ret;}



String matching algorithm

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.