网上有很多,但有bug,特别是这个:http://www.oschina.net/code/snippet_16840_2015 好大的坑...

get length

def lcs_len(a,b):
c = [[0 for j in b] for i in a]
for i in range(len(a)):
for j in range(len(b)):
if i==0 or j==0:
continue
if a[i]==b[j]:
c[i][j] = c[i-1][j-1]+1
else:
c[i][j] = max(c[i-1][j],c[i][j-1])
return c[i][j]

get string

def lcs_string(a,b):
lena,lenb = len(a),len(b) # length table
c = [[0 for i in b] for j in a] # direction table
flag = [[0 for i in b] for j in a] for i in range(lena):
for j in range(lenb):
if i==0 or j==0: continue if a[i]==b[j]:
c[i][j] = c[i-1][j-1]+1
flag[i][j] = 3 elif c[i-1][j]<c[i][j-1]:
c[i][j] = c[i][j-1]
flag[i][j] = 2
else:
c[i][j] = c[i-1][j]
flag[i][j] = 1 (p1,p2) = (lena-1,lenb-1)
s = []
while 1:
d = flag[p1][p2]
if d == 3:
s.append(a[p1])
p1 -= 1
p2 -= 1
elif d == 2:
p2 -= 1
elif d == 1:
p1 -= 1
if p1==0 or p2==0:
# solve the first element without print problem
if a[p1]==b[p2]:
s.append(a[p1])
break
s.reverse()
return ''.join(s)

use into app

def lcs_from_list(input,jobnames):
if input in jobnames:
return input
res = [(lcs_len(input,jobname),jobname) for jobname in jobnames]
res.sort()
return res[-1][1]

python 最长公共子序列的更多相关文章

  1. [Python]最长公共子序列 VS 最长公共子串[动态规划]

    前言 由于原微软开源的基于古老的perl语言的Rouge依赖环境实在难以搭建,遂跟着Rouge论文的描述自行实现. Rouge存在N.L.S.W.SU等几大子评估指标.在复现Rouge-L的函数时,便 ...

  2. 最长公共子序列python实现

    最长公共子序列是动态规划基本题目,以下依照动态规划基本步骤解出来. 1.找出最优解的性质,并刻划其结构特征 序列a共同拥有m个元素,序列b共同拥有n个元素,假设a[m-1]==b[n-1],那么a[: ...

  3. [python] 获得所有的最长公共子序列

    两句闲话 得到两个序列的最长公共子序列(LCS)是个经典问题,使用动态规划,实现起来并不难. 一般来说,我们只是输出一个LCS.但是,老师布置的作业是输出所有的LCS. 解法 按照一般的方法,我们首先 ...

  4. 用Python计算最长公共子序列和最长公共子串

    如何用Python计算最长公共子序列和最长公共子串 1. 什么是最长公共子序列?什么是最长公共子串? 1.1. 最长公共子序列(Longest-Common-Subsequences,LCS) 最长公 ...

  5. python实现查找最长公共子序列

    #!/usr/bin/python # -*- coding: UTF-8 -*- worlds = ['fosh','fort','vista','fish','hish','hello','ohd ...

  6. 【python】Leetcode每日一题-最长公共子序列

    [python]Leetcode每日一题-最长公共子序列 [题目描述] 给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度.如果不存在 公共子序列 ,返回 0 . ...

  7. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

  8. 【ZH奶酪】如何用Python计算最长公共子序列和最长公共子串

    1. 什么是最长公共子序列?什么是最长公共子串? 1.1. 最长公共子序列(Longest-Common-Subsequences,LCS) 最长公共子序列(Longest-Common-Subseq ...

  9. python 回溯法 子集树模板 系列 —— 14、最长公共子序列(LCS)

    问题 输入 第1行:字符串A 第2行:字符串B (A,B的长度 <= 1000) 输出 输出最长的子序列,如果有多个,随意输出1个. 输入示例 belong cnblogs 输出示例 blog ...

随机推荐

  1. /usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h:No such file or directory的解决办法

    在编译32位HDecode时出现如题所示的错误,原因时没有安装32位glibc库导致的: ubuntu: sudo apt-get install libc6-dev-i386 CentOS:yum ...

  2. JavaScript-闭包注意事项

    闭包允许内层函数引用父函数中的变量,但是该变量是最终值

  3. WaitAny, WaitAll 和 SignalAndWait

    除了Set 和 WaitOne方法外,在类WaitHandle中还有一些用来创建复杂的同步过程的静态方法. WaitAny, WaitAll 和 SignalAndWait使跨多个可能为不同类型的等待 ...

  4. max_input_vars 的影响

    一同事,让帮忙解决问题:post了1020条数据,结果只显示250条. 判断可能是php的post设置问题,结果发现php.ini里关于post的设置没有问题. 通过 php://input 得到请求 ...

  5. JSP 相关试题(五)

    Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definiti ...

  6. cacti

    http://www.cacti.net/downloads/docs/html/index.html Cacti脚本及模板论坛:http://forums.cacti.net/forum-12.ht ...

  7. ActionContext表格总结

    用一张表格来总结: 变量 从ActionContext中获得 生命周期 用Ongl来读取值 使用ServletConfigInterceptor来注入 ActionContext类 静态方法Actio ...

  8. Android 异步加载解决方案

    Android的Lazy Load主要体现在网络数据(图片)异步加载.数据库查询.复杂业务逻辑处理以及费时任务操作导致的异步处理等方面.在介绍Android开发过程中,异步处理这个常见的技术问题之前, ...

  9. Redis 利用锁机制来防止缓存过期产生的惊群现象-转载自 http://my.oschina.net/u/1156660/blog/360552

    首先,所谓的缓存过期引起的“惊群”现象是指,在大并发情况下,我们通常会用缓存来给数据库分压,但是会有这么一种情况发生,那就是在一定时间 内生成大量的缓存,然后当缓存到期之后又有大量的缓存失效,导致后端 ...

  10. HDP2.4安装(六):小结与回顾

    基于Centos7安装过程中常用工具及操作技术总结回顾. 操作技巧: tab键,命令自动补全 xshell 默认: Ctrl + Insert (复制)  Shift + Insert (粘贴) sy ...