Find the substring-kpm pattern matching-NFA/DFA

Source: Internet
Author: User

To find a child string

There are 5 kinds of minimal operations subsets in data structure: string assignment, string comparison, string length, string connection, substring , and other operations can be implemented on this subset.

    • Pattern matching of strings in data structure
KPM Pattern Matching algorithm

A basic pattern matching algorithm

//substring the position of a string in string
functionsubString (String, subString) {varI=0,j=0;
When I or j out of range exits while(i<string.length&&j<substring.length) { if(string[i]==Substring[j]) { ++i;++J}Else{
When the match is unsuccessful, I is moved from the start position to one I=i-j+1;j=0; } }
If J is out of range then return i-j, if I out of range means not found
if (j>=substring.length) return i-J; Else return false;
}

See, whenever the match is unsuccessful, I always return to the beginning of this match
KPM, an improved pattern matching algorithm to solve the I-back problem

This leads to a very important question ' including the prefix ',

Take substring= 'abcabcacab', for example. Prefix string ' abca ' = ' abc[abca]Cab ' in square brackets, if the match fails at c after the string, simply make the prefix string a with a in parentheses, and then continue to match from C where the match failed.

So we need to find out the value of the substring to move back to the j-k+1 after the match failure in J

Suppose that F (j) represents the largest k in the substring prefix preceded by J, such as ' ABCABCA ', k-1=1,4, i.e. the maximum K is 5;f (8) =5

So ' ABCABCACab ', F (9) =f (8) + (Substring[5]? ==SUBSTRING[8]), if equal, then f (9) = 6; if not equal F (9) requires recalculation, because C of substring[9-1] causes the maximum inclusion prefix to no longer be ABCA, but one containing prefix string that has the end of C. There is actually no such string;

blog.csdn.net/yukuninfoaxiom/article/details/6057736

blog.csdn.net/joylnwang/article/details/6778316/

http://www.rudy-yuan.net/archives/182/

Www.webhek.com/misc/comparison-sort

    • Compiler principle Lexical analyzer
Nfa/dfa

Find the substring-kpm pattern matching-NFA/DFA

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.