KMP algorithm for Pattern Matching

Source: Internet
Author: User


For common string matching, if the length of the mode string is n and the length of the source string is m, the source string is matched from the beginning. The two pointers I to the source string, and J to the mode string, in case of any difference, backtracing causes J to = 0, so repeated matching reduces the efficiency.

Because the pattern strings before I match the matched strings are the same, that is, during backtracking, the pattern strings do not need to be matched with the source strings, only match the pattern string with itself to determine whether to backtrack and where to trace the pattern string. Then we can perform self-matching on the pattern string before matching the pattern string to calculate the position that is traced back after I fails to match any position of the pattern string.

Another optimization of the Self-matching algorithm is that the pattern string matches its position a at position B, and then determines whether the characters in these two locations are the same. If they are the same, then, the Backtracking position of Position A is assigned to B. If there are different values, it means there is no need to trace back to this position. Because it does not match after backtracking, it is directly set to 0, indicates rematching from 0,

The Return Value of the failed function is-1, which is used to set its structure, so that it can simply implement its function in self-matching, and identify the failure to start again, however, in the pattern matching, the effect is the same as that of 0, where I is set to 0, J ++, and then the matching continues.

Put self-matching in a failed function, operate on the mode string, and then put the result in an array for convenient query.

Try to write c ++ code:


Here, the duration is to judge that if the I-th string is the same, then if I-th adds a string that does not match, the position to be returned is to operate on I + 1 each time, therefore, the last operation will be performed on the characters marked as N in the array, and the operation will be out of bounds. when creating the Fail array, consider it.



void FailString(int f[],const char* str){int length = strlen(str);int i =0, k = -1;f[0] = -1;while( i < length){if(k == -1 || str[i] == str[k]){i++;k++;if(str[i] == str[k])f[i] = f[k];elsef[i] = k;}elsek = f[k];}}bool compareString(const char* charA,const char* charB){if( charA == 0 || charB == 0)return false;int Alen = strlen(charA);int Blen = strlen(charB);if( ! Alen || ! Blen )return false;if( Blen > Alen)return false;int* fail = new int[ Blen+1 ];FailString(fail,charB);int i = 0 , j = 0;while(i < Alen && j < Blen){if( charA[i] != charB[j] ){if(fail[i] == -1 )j = 0;elsej = fail[i];}elsej ++;i++;}delete[] fail;fail =NULL ;return j==Blen;}void main(){char* s1 = "abcabcaabcabbac";char* s2 = "abcabcabbac";if(compareString(s1,s2))cout<<"s1==s2"<<endl;cin.get();}


KMP algorithm for Pattern Matching

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.