005_013 Python 寻找子序列 字符串应该用find 否则用KMP
来源:互联网 发布:mac spark 环境搭建 编辑:程序博客网 时间:2024/05/22 04:43
代码如下:
#encoding=utf-8print '中国'#寻找子序列#字符串应该用find 否则用KMPdef KnuthMorrisPratt(text, pattern): pattern = list(pattern) length = len(pattern) shifts = [1] * (length + 1) shift = 1 for pos, pat in enumerate(pattern): while shift <= pos and pat != pattern[pos-shift]: shift += shifts[pos-shift] shifts[pos+1] = shift startPos = 0 matchLen = 0 for c in text: while matchLen == length or matchLen >= 0 and pattern[matchLen] != c: startPos += shifts[matchLen] matchLen -= shifts[matchLen] matchLen += 1 if matchLen == length: yield startPosfor i in KnuthMorrisPratt([1,2,3,4,2,3],[2,3]): print i
打印结果如下:
中国
1
4
0 0
- 005_013 Python 寻找子序列 字符串应该用find 否则用KMP
- 001_013 Python 字符串分割 访问子字符串
- python实现寻找最长回文子序列
- python对指定字符串寻找回文子序列的最小划分
- 用二分法寻找最长连续单调递增子序列
- 关于寻找两个字符串中最长子序列的问题
- 寻找子序列
- 寻找最长递增子序列
- 寻找最长递增子序列
- 寻找最长递增子序列
- 寻找最长递增子序列
- 在大字符串寻找共出现几次子串的位置(KMP算法C写法)
- 用Python寻找质数
- 寻找最大公共子字符串
- 寻找最大公共子字符串
- 寻找最大重复子字符串
- 寻找最长公共子字符串
- 算法题目二:寻找最长重复子序列(4) 求给定字符串重复最长的子串
- drupal7自定义输出节点内容($content里的每一个字段)
- POJ3104_DRYING_二分
- Android记事本开发之SQLite数据库实现
- CC2530与zigbee学习笔记の初识zigbee(3)
- python进阶七_文件操作(一)
- 005_013 Python 寻找子序列 字符串应该用find 否则用KMP
- [连载]C#程序设计(14)--- 泛型和容器
- 编程之美热身赛 题目1 : 传话游戏
- POJ2479 Java 动态规划
- POJ3104_Pie_二分
- 记忆力游戏(C#版)
- multimap应用
- ubuntu下的nginx+php+mysql配置
- 对C语言类型转换总结及求校验和的教训