KMP algorithm for implementing string in Python

Source: Internet
Author: User

In this paper, we describe the KMP algorithm of Python implementation string. Share to everyone for your reference, as follows:

KMP Algorithm Python implementation

Today research KMP algorithm, it seems many versions, there are different languages written, but feel more chaotic, finally try to write a copy of the summary


First, the KMP algorithm makes the optimization algorithm in string matching, so that the original O (m*n) drops to O (m+n)

As for his understanding, I recommend watching the video first, he speaks it very clearly. Wang can understand. KMP String matching algorithm
Then from the visual aspects of understanding, recommended to see how to better understand and master the KMP algorithm? -The answer of the Blessed son
Finally understand searching for Patterns from the code layer | Set 2 (KMP algorithm)
Code do not see too many others, I feel a lot of people write also have problems, I run the problem, I have to see some students write, was taken to other pits ...
Last recorded code

"' first seek Next array ' Def next_list (pattern): next=[] pattern_list=list (pattern) j=0 I=1 for S in range (len (pattern ): #第一位一直是0 if S==0:next.append (0) #看第i个和第j个字母是否相同, if same, accumulate #同时i, J simultaneously right-move Eli F (Pattern_list[i]==pattern_list[j]): Next.append (j+1) j+=1 i+=1 #如果不相同, Then next is 0, and J also returns to the first position, I move forward a position else:next.append (0) j=0 i=s+1 print (next) RET Urn nextnext_list (' ababcabab ') def KMP (text,pattern): #text的位置 i=0 #pattern的位置 j=0 next=next_list (Patt     Ern) if (not (text and pattern)): print (' String null, enter String ') elif (len (text) <len (pattern)): Print (' Original string too small ') Elif (Text==pattern): print (' string consistent ') Else:while ((I<len (text))): Print ((text[i],patter                N[J]) print (i,j) #如果相同, then the next contrast if (Text[i]==pattern[j]): i+=1 J+=1 #判断Is it a match? If J==len (pattern): print (' start matching from {0} '. Format (I-J)) j=next[j-1] #如果不匹配, J back to the previous letter corresponding to the next elif I<len (text) and Text[i]!=pattern[j]: #我就是少了这个判断, there has been a problem, is not the horse After the second step, after this very critical if j!=0: #这个就是精髓了, if not match, go to find the first and this match the string, and then in this front of the matching string #的后一个字母拿                    Out, and then compared with the long text of the first letter of comparison j=next[j-1] #如果j已经回到了0, then by increasing the i,text to move to the next letter else: i+=1# Text = "Ababdabacdababcabab" # pattern = "Ababcabab" text= ' Abxabcabcaby ' pattern= ' abcaby ' km  P (text,pattern) #output: next=[0, 0, 0, 1, 2, 0] (' A ', ' a ') 0 0 (' b ', ' B ') 1 1 (' X ', ' a ') 2 0 (' A ', ' a ') 3 0 (' B ', ' B ') 4 1 (' C ', ' C ') 5 2 (' A ', ' a ') 6 3 (' B ', ' B ') 7 4 (' C ', ' C ') 8 2 (' A ', ' a ') 9 3 (' B ', ' B ') 4 (' Y ', ' y '), and the 6th position starts matching

Related recommendations:

KMP algorithm is the most superficial understanding

KMP algorithm Detailed

Understanding of the most difficult place to understand in the KMP algorithm

Principle and implementation of KMP algorithm

Graphical KMP algorithm

Related Article

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.