Python实现字符串匹配的KMP算法
kmp算法
KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt同时发现,因此人们称它为克努特――莫里斯――普拉特操作(简称KMP算法)。KMP算法的关键是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的。具体实现就是实现一个next()函数,函数本身包含了模式串的局部匹配信息。
#! /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 #移动位数 = 已匹配的字符数 C 对应的部分匹配值 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()
总结
相关推荐
数据与算法之美 2020-06-10
yuanran0 2020-05-11
shawsun 2020-05-10
bluewelkin 2020-05-06
shenwenjie 2020-04-11
horizonheart 2020-03-05
nurvnurv 2020-02-02
hanyujianke 2020-01-12
Happyunlimited 2019-11-08
极乐净土 2014-07-17
dushine00 2019-06-26
duyifei0 2019-06-21
tieshow 2013-01-18
Wendywubowen 2018-07-07
数据与算法之美 2019-04-29
PythonBiglove 2015-07-18
大腕绿茶 2013-05-03
BitTigerio 2018-04-17