字符串匹配的KMP算法——Python實現


#! /usr/bin/python
# coding=utf-8
"""
基於這篇文章的python實現
http://blog.sae.sina.com.cn/archives/307
"""
import unittest

def pmt(s):
    """
    PartialMatchTable
    """
    prefix = [s[:i+1] for i in range(len(s)-1)]
    postfix = [s[i+1:] for i in range(len(s)-1)]
    intersection = list(set(prefix) & set(postfix))
    if intersection:
        return len(intersection[0])
    return 0
def kmp(big,small):
    i = 0
    while i < len(big) - len(small) + 1:
        match = True
        for j in range(len(small)):
            if big[i+j] != small[j]: 
                match = False
                break
        if match:
            return True
        #移動位數 = 已匹配的字符數 – 對應的部分匹配值
        if j:
            i += j - pmt(small[:j])
        else:
            i += 1
    return False

class kmpTests(unittest.TestCase):
    def test_pmt(self):
        self.assertEqual(pmt("A"),0)
        self.assertEqual(pmt("AB"),0)
        self.assertEqual(pmt("ABC"),0)
        self.assertEqual(pmt("ABCD"),0)
        self.assertEqual(pmt("ABCDA"),1)
        self.assertEqual(pmt("ABCDAB"),2)
        self.assertEqual(pmt("ABCDABD"),0)
        self.assertEqual(pmt("AAAAAA"),5)
    def test_kmp(self):
        self.assertTrue(kmp("ABCD","CD"))
        self.assertFalse(kmp("ABCD","BD"))
        self.assertTrue(kmp("BBC ABCDAB ABCDABCDABDE","ABCDABD"))
        
if __name__ == '__main__':
    unittest.main()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM