Objective:
To resolve string pattern matching
Course:
Naïve pattern matching: successive comparisons
KMP algorithm: Use the information obtained from the matching failure to maximize the movement of the pattern string, in order to reduce the number of comparisons to improve performance
Concept:
M: is the target string length
N: is the pattern string length
J: When a match occurs, the first occurrence of the different index position (some are called: mismatch )
K: Longest string length (sometimes referred to as: the longest common prefix )
Core idea:
S S0 S1 ... Si-j-1 si-j si-j+1 si-j+2 ... Si-2 Si-1 Si ... Sn-1
|| || || || || X
P P0 P1 P2 Pj-2 Pj-1 Pj
There are si-j-1si-jsi-j+1 si-j+2 ... Si-2 si-1=p0p1 P2 ... Pj-2 Pj-1
If p0p1 P2 ... Pj-2≠p1 P2 ... Pj-2pj-1
You can immediately conclude that p0p1 P2 ... Pj-2≠si-j+1 si-j+2 ... Si-2 Si-1, i.e.: The next move of the naïve pattern match must not match, you can skip this one time
If p0p1 P2 ... Pj-3≠p2 ... Pj-2pj-1
You can immediately conclude that p0p1 P2 ... Pj-2≠si-j+1 si-j+2 ... Si-2 Si-1, i.e.: The next move of the naïve pattern match must not match, you can skip this one time
Until the first occurrence of an equal condition terminates:p0p1 P2 ... Pk-1 = Pj-k ... Pj-2pj-1
The resulting k is the longest end-to-end string length, and then we get the number of bits that we need to move through j-k, so we take advantage of the result of the match failure, get the number of steps we can move, improve the performance
About K:
In fact, the naked eye can be seen directly, K is the longest string length, such as:
11111 k=4 (prefix: 1111, suffix: 1111)
12112 k=2 (prefix: 12, suffix: 12)
12345 k=0 (no same prefix suffix)
Example:
S=ababababababb
P=abababb
To reiterate the principle: the low efficiency of simple pattern matching is due to a comparison of one person, discarding the previously failed information. The KMP algorithm can reduce the number of comparisons to improve performance by obtaining the maximum number of steps to be moved from the matching failure information.
There is no mention of the next array and the Newnext array, the characteristic vector N of the pattern string, in fact, it does not matter, the thought understands, but someone else has made a term.
Java code:
/*** Simple pattern matching * *@paramSource Target String *@paramPattern Mode String*/ Private Static voidPlain (string source, string pattern) {intRes=0; intSourcelength=source.length (); intPatternlength=pattern.length (); for(intI=0;i<= (sourcelength-patternlength); i++) {res++; String Str=source.substring (i, i+patternlength); if(Str.equals (pattern)) {p ("Naïve mode: Match success"); Break; }} p ("Plain mode: Match" +res+ "number of times"); }
KMP Algorithm Implementation
Private Static voidKMP (string source, string pattern) {int[] n=GETN (pattern); intRes=0; intSourcelength=source.length (); intPatternlength=pattern.length (); for(intI=0;i<= (sourcelength-patternlength);) {res++; String Str=source.substring (i, i+patternlength);//the string to comparep (str); intCount=GetNext (pattern, str,n); P ("Move" +count+ "step"); if(count==0) {p ("KMP: Match succeeded"); Break; } I=i+count; } p ("KMP: Match" +res+ "number of times"); } /*** Get the next number of times to move * *@paramPattern *@paramSTR *@paramN *@return0, string matching;*/ Private Static intGetNext (String pattern,string str,int[] N) {intn =pattern.length (); CharV1[] =Str.tochararray (); CharV2[] =Pattern.tochararray (); intx = 0; while(n--! = 0) { if(V1[x]! =V2[x]) { if(x==0){ return1;//If the first one is not the same, move 1 steps } returnX-N[X-1];//x: The position of the first occurrence of a different index, i.e. J} x++; } return0; } Private Static int[] GETN (String pattern) {Char[] pat=Pattern.tochararray (); intJ=pattern.length ()-1; int[] n=New int[J+1]; for(inti=j;i>=2;i--) {N[i-1]=GETK (I,PAT); } for(inta:n) p (a); returnN; } Private Static intGETK (intJChar[] Pat) { intX=j-2; intY=1; while(X>=0 && Compare (Pat, 0, X, y, j-1) ) {x--; Y++; } returnX+1; } Private Static BooleanCompareChar[] Pat,intB1,intE1,intB2,intE2) { intn = e1-b1+1; while(n--! = 0) { if(PAT[B1]! =PAT[B2]) { return true; } B1++; B2++; } return false; } Public Static voidp (Object obj) {System.out.println (obj); }
Next array:
KMP can improve performance because it reduces the number of comparisons, which is known as K
and k is only related to J, which means that the number of moves is only related to the pattern string, not to the target string.
Simply put, we can immediately know the number of moves after we get the pattern string, which is the next array. It's a K-value stored inside.
KMP Algorithm-java Implementation