网上有很多,但有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. CoordinatorLayout-带图片伸缩工具栏

    伸缩工具栏toolbardesign 效果图: 步骤一: 在build.gilde中添加以下代码 dependencies { compile fileTree(dir: 'libs', includ ...

  2. Oracle数据库——数据库安全性管理

    一.涉及内容 1.验证系统权限管理. 2.验证角色管理. 3.验证概要文件管理. 二.具体操作 (一) 1.根据以下要求进行系统权限的授予与回收操作. (1)创建用户user1,并为它授予create ...

  3. 从代码看 asp.net 处理过程

    从这里开始 先是一个 对Com接口的导入.   /// <internalonly/>    /// <devdoc>    /// </devdoc>    [C ...

  4. linux安全

    http://drops.wooyun.org/ 最专业的安全知识分享平台 http://www.freebuf.com/ 关注黑客与极客 http://book.beifabook.com/prod ...

  5. Exploring the Angular 1.5 .component() method

    Angular 1.5 introduced the .component() helper method, which is much simpler than the.directive() de ...

  6. Linux 下SVN自动更新

    1.找到svn的所在目录: 我的目录在/usr/local/svn/myproject 2.新建post-commit vim hooks/post-commit #!/bin/shexport LA ...

  7. SpringQtz 时间任务调度

    1.配置所需要maven jar包 <!-- 任务调度需要的jar包--> <dependency> <groupId>org.quartz-scheduler&l ...

  8. 如何进行oracle capability i/o(压力测试数据库服务器i/o性能)

    一 .oracle 有关 IO 信息的相关统计函数 Oracle i/o stack包含hbas,存储相关的交换机.存储物理磁盘.那么oracle建议在应用程序部署的时候,建议去验证i/o避免存在问题 ...

  9. openoffice下中文乱码问题解决

    在系统安装完OpenOffice进行pdf文档转换的时候 文档会出现乱码如下: 这里主要有两个原因: 1.上传的文件的编码是Windows上的默认编码GBK,而linux服务器默认编码是UTF-8,这 ...

  10. 内容模块PC标签调用说明

    内容模块 模块名:content 模块提供的可用操作 操作名 说明 lists 内容数据列表 relation 内容相关文章 hits 内容数据点击排行榜 category 内容栏目列表 positi ...