After the completion of the harem problem (eight queen problem), and the use of a half-day time to complete the famous "Look at the--KMP" algorithm. This is definitely a big hole for beginners, it's very difficult to understand.
In this, to the proposed KMP algorithm of the three big guys to express sincere respect.!!! Cow x!!!
first, let's introduce what is the KMP algorithm : KMp Count method is an improved string matching algorithm found by D.e.knuth,j.h.morris and V.r.pratt, so it is called the Knut-Morris-Pratt Operation (abbreviated as KMP algorithm). The key of KMP algorithm is to reduce the number of matches between the pattern string and the main string so as to achieve fast matching by using the information after the match failure. Implementation is the implementation of a next () function, the function itself contains the pattern string local matching information. Time complexity O (m+n). [1]
In a WORD:!! Knock the blackboard!!! , in the case of string matching, there are common masses thinking and genius youth thinking
ordinary masses of thinking is a violent crack, will be in the main string all with the length of the pattern string of continuous substring and pattern string comparison, in this process, the main string index there will be a fallback process, greatly pull down the efficiency.
Genius Youth Thinking is KMP match, in the match, the main string index has been forward, not back, only change the index of the pattern string, so that the matching efficiency is greatly improved.
In the KMP algorithm, there are two major problems to understand: the kmp principle and Next array calculation , the next will be in the two parts as far as possible to explain.
KMP principle
First of all, I put an article I think of the better blog information, as a small white I, but also look at the blog 1.1 slowly learning. http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html.
This blog post I think is enough for beginners to understand the principles of KMP. The core idea is that:
There is a comparison pointer in the main string and the pattern string, where the pointer refers to the place where the program is compared, the use of the information of the pattern string to grasp, so that in the match with the main string, the flexibility to change the comparison pointer, so that the main string of the comparison pointer has been forward, never back.
With tomb-Raider notes in a nutshell summed up that, "small three ye boldly go forward ah, go forward, MO back ah head."
In this, the key is how to move the position of the comparison pointer in the matching process? This is the famous next array .
Next array
Read the Internet so many explanations, I think this version of the interpretation of the best understand, really Lanqueillan http://www.cnblogs.com/tangzhengyue/p/4315393.html, because of their own level is limited, So there's not much to say, I'm afraid the more I say the more confused. The key is to calculate each letter in the pattern string
The longest prefix match. The explanation of the prefix has been written very clearly in the IT that introduced the KMP principle.
The following directly on the code bar, so that more direct.
Code
defGetNext (t): J, I=-1, 0 next= [ -1]*len (t) whileI < Len (t)-1: if-1 = = JorT[i] = =T[j]: I, J= i + 1, j + 1Next[i]=JElse: J=Next[j]returnNextdefKMP (s,t): Next=GetNext (t) j, I=-1,-1 whileJ! = Len (t) andI <Len (s):ifS[i] = = T[j]orj = =-1: I, J= i + 1, j + 1Else: J=Next[j]return(i-j,true)ifj = = Len (t)Else "None"Print(KMP ("ababxbabcdabdfdsss","ABX"))
Originally tried to explain the program, but found it really difficult to explain, next array of the solver of the most critical is how to initialize. The KMP program is relatively good to understand.
Well, the best way to do this is to put the code in the IDE and step through it, each of you performing a view of the variable, so that the principle is understood the quickest.
[1]. Excerpt from Baidu Encyclopedia KMP entry
Python----KMP (post recommendation + code)