KMP algorithm Detailed

Source: Internet
Author: User

The KMP algorithm is mainly used to solve the inclusion problem, that is, there are two strings str1 and str2, to determine whether the STR1 string contains the string str2, contains the str2 corresponding to the first character of the string in str1, otherwise return-1;

For example: str1= "Abc123def" str2= "123d", str1 length is n, str2 length is M, then 3 is returned;

So how to solve this kind of problem? The normal solution takes a time complexity of O (n*m), that is, one character to match, the match is unsuccessful, then the STR1 index moves backward one bit, str2 re-matches from the first character.

The time complexity for solving this problem using the KMP algorithm is O (N), and the default m<n, such as M>n, do not match.

Before introducing the KMP algorithm, let's introduce the longest prefix and the longest suffix:

For example str= "ABCABCD" for the length of the longest prefix and longest suffix of the character ' d ', first the prefix array and the suffix array of the character ' d '

The prefix array of D: "A", "AB", "abc", "ABCA", "Abcab" prefix in the array of strings that do not contain the last character (i.e. ' d ' of the previous character ' C ')

The suffix array of D: "C", "BC", "abc", "Cabc", "bcabc" suffix in the string in the array does not contain the first character (that is, the first character ' a ')

From the above can be seen the longest prefix of the character ' d ' and the longest suffix of the match length is 3, that is, "ABC", we define the ' d ' character position (belonging to position 6) of the next[6]=3;

Let's ask for next[0],next[1],next[2],next[3],next[4],next[5].

There are no prefixes and suffixes in the 0 position and 1 position, so we define next[0]=-1,next[1]=0;

For position 2 (character ' C '), we can see that the prefix array is: "A", the suffix array is: "B", obviously the longest prefix and the longest suffix of the matching length of 0, so next[2]=0;

For position 3 (character ' a '), we can see that the prefix array is: "A", "ab", the suffix array is: "C", "BC", obviously the longest prefix and the longest suffix of the matching length of 0, so next[3]=0;

For position 4 (character ' B '), we can see that the prefix array is: "A", "AB", "abc", the suffix array is: "A", "Ca", "BCA", obviously the longest prefix and the longest suffix of the matching length of 1, so next[4]=1;

For position 5 (character ' C '), we can see that the prefix array is: "A", "AB", "abc", "ABCA", suffix array: "B", "AB", "cab", "Bcab", obviously the longest prefix and longest suffix match length is 2, so next[5]=2;

The source of the next array in the KMP algorithm explained above, then how do we use the more excellent algorithm to find the next array?

1) ask for next[i], need according to Next[i-1], first, define CN = Next[i-1], determine str[i-1] is equal to STR[CN],

As example 1:str[i-1]==str[cn]= ' C ', then next[i]=cn+1=3, here cn=next[i-1]=2

As examples 2 and 3:str[i-1]! =STR[CN], that is ' c '! = ' t ', ' c '! = ' A ', here CN =next[i-1]=4, this time CN forward, that is CN = next[4]=2

Continue to compare str[i-1] is equal to STR[CN], as shown in examples 2 and 3, example 3 shows ' a ' = = ' A ', so for example 3,next[i]=cn+1=3,

Example 2 shows ' a '!=t, so CN continues to jump forward, cn=next[cn]=next[2]=0;

Example 2 continue to compare str[i-1] is equal to STR[CN], at this time ' a '! = ' t ', CN continue to jump before, found can not jump forward, so next[i]=0

The next array code looks like this:

//construct the next array of str2, stipulating the next[0]=-1,next[1]=0; //ABABCABABTK ==> ababc ABABTK, verified that the following code is correct     Public Static int[] getnextarrays (String str) {Char[] STRs =Str.tochararray (); int[] Next =New int[Str.length ()]; intCN = 0;//for the forward jump positionNext[0] = 1; next[1] = 0; inti = 2;  while(I <next.length) {if(Strs[i-1] = =STRS[CN]) {Next[i]= Cn+1; I++; CN++; } Else if(CN > 0) {cn= NEXT[CN];//Jump forward .}Else{Next[i]= 0; I++; }        }        returnNext; }

After you have finished str2 next array, how do you find str2 in the str1 string position? How do we use the next array to speed up the process?

KMP algorithm Detailed

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.