I also learn algorithm-KMP algorithm

Source: Internet
Author: User

All along, the algorithm is more theoretical than practical, not even practical.

Recently due to project needs. A new look at the KMP algorithm. Alas, I hate this passive learning process.

But the KMP algorithm is still very interesting, it took two days to finally understand. The period references the blog post and data structure on the Internet. below to share the experience of the KMP algorithm.

The general idea of KMP is to use the characteristics of the pattern string itself to optimize the matching steps. How to use its own characteristics, KMP with an array, that is, the next array mentioned in most tutorials. I'll show you how the next array is built and used.

As mentioned earlier, the KMP algorithm needs a pattern string to meet certain conditions, then what is the condition? Here is a direct reference to the equation in the data structure book:

When K < J, T1T2...TK-1TK = tj-(k-1) tj-k ... tj-1 TJ.

Without this equation, the KMP algorithm is tantamount to wasting the most common string matching algorithm for next-size space.


Here's a picture of how this equation works when matching substrings:

(This image assumes that the array subscript starts with 1) First we find the equation: t1t2 = t6t7. So when T8 does not match, the KMP algorithm automatically aligns T1 T2 and matches it with T3 and the parent string.


So according to the above analysis, we can draw the following several conclusions (may be a little hasty, students still need to combine the textbook to understand)

1. The subscript for next array and pattern string is one by one corresponding.

2. Each character corresponding to next holds the subscript of the prefix whose previous character matches. As you can see from the above figure, Next[8] saved is 3.

3. The first letter has no prefix, so its next holds an invalid subscript. In actual programming, can be-1

4. If the substring of the equation is not in front of a character, its next holds the subscript of the first letter of the pattern string.


According to our conclusion, the implementation of the KMP algorithm is given below:

void Kmp_get_next (char *pattern, int next[]) {int i = 0, j = 0;        Next[i] =-1;            while (I < strlen (pattern)-1) {if (j = = I | | pattern[i] = pattern[j]) {if (J! = i)            {j + +;                        } i++;        Next[i] = j;        } else if (j! = 0) {j = next[j];        } else {i++;     }}}void Kmp_print_next (char *pattern, int next[]) {int i = 0;    if (NULL = = pattern) {return;    } printf ("%s ' next array is: \ n", pattern);    for (i = 0; I < strlen (pattern); i++) {printf ("%d\t", Next[i]);     }}int Kmp_is_match (Char *pattern, char *basestr, int next[]) {int i = 0;     int j = 0;            while (I < strlen (basestr)) {if (basestr[i] = = Basestr[j]) {if (j = = strlen (pattern))            {return 0;            } i++;        j + +;     } else   {j = next[j];            if (j = =-1) {i++; }}} return-1;}     int Kmp_get_match (char *pattern, char *basestr, int next[]) {int i = 0;     int j = 0;            while (I < strlen (basestr)) {if (basestr[i] = = Basestr[j]) {if (j = = strlen (pattern))            {return i;            } i++;        j + +;            } else {j = next[j];            if (j = =-1) {i++; }}} return-1;}

#include "kmp.h" int main (int argc, char **argv) {    int ret = 0;    int i = 0;    Char *pattern = "ABAABC";    int next[10] = {0};    Char *basestr = "abcabcabcabccacabdabaabcabceadb";    Kmp_get_next (pattern, next);    printf ("%s matched\n", Kmp_is_match (Pattern, basestr, next) = = 0? ' Is ': "not");    ret = Kmp_get_match (pattern, basestr, next);    return 0;}

The KMP algorithm is not difficult to understand, but it is difficult to construct the next array. At least for me. Once the array is constructed, the rest is done.

May be I understand not deep enough to write before I think there is a lot of content to share, but began to write after the discovery do not know what to write. Hope to be able to inspire students.




I also learn algorithm-KMP 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.