网上有很多,但有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. Syscall param open(filename) points to unaddressable byte(s)

    valgrind 调试出现如题所示的错误,原因是存取文件名的空间被释放了 源代码: cfg->snteam_cfg->snt.score.nd.wrd_dict_db_fn=cfg-> ...

  2. MiniCRT 64位 linux 系统移植记录:64位gcc的几点注意

    32位未修改源码与修改版的代码下载: git clone git@github.com:youzhonghui/MiniCRT.git MiniCRT 64位 linux 系统移植记录 MiniCRT ...

  3. Linux-配置虚拟IP实例

    在日常linux管理工作中,需要为应用配置单独的IP地址,以达到主机与应用的分离,在应用切换与迁移过程中可以做到动态切换,特别是在使用HA的时候,这种方案可以保证主机与应用的隔离,对日常的运维有很大的 ...

  4. Git-rebase与merge小结

    git merge是用来合并两个分支的. git merge b # 将b分支合并到当前分支 同样 git rebase b,也是把 b分支合并到当前分支 ---------------------- ...

  5. 套接字I/O模型-WSAAsyncSelect

    利用这个异步I/O模型,应用程序可在一个套接字上接收以Windows消息为基础的网络事件通知.WSAAsyncSelect和WSAEventSelect提供读写数据能力的异步通知,但它们不提供异步数据 ...

  6. 自动生成XML反序列化的类

    原文地址:http://www.cnblogs.com/jaxu/p/3632077.html   Visual Sutdio 2013增加了许多新功能,其中很多都直接提高了对代码编辑的便利性.如: ...

  7. 【转】ASP.NET Cookies简单应用 记住用户名和密码

    不要试图给Password类型的TextBox赋值! 在asp.net中,不要试图给Password类型的TextBox控件赋值! 无论是在设计或是运行时,都不可以的. 猜测的原因是,password ...

  8. php面向对象中的魔术方法中文说明

    1.__construct() 实例化对象是被自动调用.当__construct和以类名为函数名的函数 同时存在时调用__construct,另一个不背调用. 类名为函数名的函数为老版的构造函数. 2 ...

  9. ExtJs学习之Window

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  10. 我对javascript的自以为是

    参数的作用域 if(true){ var tester = "Hello World"; } console.log(tester); 按照以往的经验,觉得上面的代码会报错,而实际 ...