KMP-字符串匹配算法 Python 2.7实现

# Python Interpreter: Python 2.7.2
# Function: using KMP search method to find whether string A is in string B
# Reference Video: https://www.bilibili.com/video/av11866460
# Author: Qiankun Wang
# Data: 2018.9.12
class KMP:
    # build prefix table using shorter strings firstly
    def prefix_table(self,pattern,n): 
        len = 0
        i = 1
        while i < n:
            # print len,i
            if pattern[i] == pattern[len]:
                len += 1
                prefix[i] = len
                i += 1
            else:
                if len > 0:
                    len = prefix[len - 1]
                else:
                    prefix[i] = len
                    i += 1
        return prefix
    #  move the prefix table one step backward
    def move_prefix_table(self,pattern,n): 
        for i in range(n-1,0,-1):
            prefix[i] = prefix[i-1]
        prefix[0] = -1
        return prefix

    # judge whether pattern is in text using method of kmp search
    def kmp_search(self,pattern,text):
        i = 0 # corresponding to the index of text(longer strings)
        j = 0 # corresponding to the index of pattern(shorter strings)
        flag = 0 
        while i < m:
            if j == n - 1 and text[i] == pattern[j]:
                flag = 1
                # print index at which pattern lies in text
                print "Find pattern at index %d of text"%(i-j) 
                j = prefix[j]
            if text[i] == pattern[j]:
                i += 1
                j += 1
            else:
                j = prefix[j]
                # reach at the starting point,move both index i and j one step backward
                if j == -1: 
                    i += 1
                    j += 1
        if flag == 0:
            print "pattern is not in text"


if __name__ == "__main__":
    pattern = 'ABABCABAA'
    text = 'ABABABCABAABABABAB'
    n = len(pattern)
    m = len(text)
    prefix = [0] * n
    s = KMP()
    s.prefix_table(pattern,n)
    s.move_prefix_table(pattern,n)
    s.kmp_search(pattern,text)

你可能感兴趣的:(算法,算法,Python,字符串)