国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python字符串匹配算法KMP實例

2019-11-25 17:09:55
字體:
供稿:網(wǎng)友

本文實例講述了Python字符串匹配算法KMP。分享給大家供大家參考。具體如下:

#!/usr/bin/env python#encoding:utf8def next(pattern):p_len = len(pattern)pos = [-1]*p_lenj = -1for i in range(1, p_len):while j > -1 and pattern[j+1] != pattern[i]:j = pos[j]if pattern[j+1] == pattern[i]:j = j + 1pos[i] = jreturn posdef kmp(ss, pattern):pos = next(pattern)ss_len = len(ss)pattern_len = len(pattern)j = -1for i in range(ss_len):while j > -1 and pattern[j+1] != ss[i]:j = pos[j]if pattern[j+1] == ss[i]:j = j + 1if j == pattern_len-1:print 'matched @: %s' % str(i-pattern_len+1)j = pos[j]kmp(u'上海自來水來自海上海', u'上海')

希望本文所述對大家的Python程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 东丰县| 伊金霍洛旗| 松溪县| 永新县| 兴山县| 北碚区| 扎鲁特旗| 休宁县| 镇原县| 胶南市| 南江县| 温宿县| 古丈县| 简阳市| 亳州市| 拜泉县| 淳化县| 云龙县| 锡林郭勒盟| 大化| 彭阳县| 集安市| 普兰店市| 井研县| 淅川县| 都匀市| 皮山县| 巴彦淖尔市| 建宁县| 伊川县| 肥东县| 句容市| 澳门| 丰城市| 遂宁市| 嘉荫县| 鄂尔多斯市| 桂阳县| 淮滨县| 涟水县| 大英县|