Basic implementation of the KMP algorithm, C + +

Source: Internet
Author: User

The implementation of the KMP algorithm is based on the introduction of the algorithm in section 32.4.

int* GenerateNext (String &p) {    const int m = P.length ();    int *next = new Int[m];    int k =-1;    Next[0] =-1;    for (int i = 1, i < m; i++)    {while        (k>-1 && p[k + 1]! = P[i])            k = next[k];        if (p[k + 1] = = P[i])            k++;        Next[i] = k;    }    return next;} void Kmp_matching (String&p, string&t) {    const int m = P.length ();    const int n = t.length ();    int *next = GenerateNext (p);    int k =-1;    for (int i = 0, i < n; i++)    {while        (k>-1 && p[k + 1]! = T[i])            k = next[k];        if (p[k + 1] = = T[i])            k++;        if (k = = m-1)        {            cout << "Found it at" << i-k << Endl;            k = Next[k];}}    

  

Basic implementation of the KMP algorithm, C + +

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.