python 最长公共子序列
网上有很多,但有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 最长公共子序列的更多相关文章
- [Python]最长公共子序列 VS 最长公共子串[动态规划]
前言 由于原微软开源的基于古老的perl语言的Rouge依赖环境实在难以搭建,遂跟着Rouge论文的描述自行实现. Rouge存在N.L.S.W.SU等几大子评估指标.在复现Rouge-L的函数时,便 ...
- 最长公共子序列python实现
最长公共子序列是动态规划基本题目,以下依照动态规划基本步骤解出来. 1.找出最优解的性质,并刻划其结构特征 序列a共同拥有m个元素,序列b共同拥有n个元素,假设a[m-1]==b[n-1],那么a[: ...
- [python] 获得所有的最长公共子序列
两句闲话 得到两个序列的最长公共子序列(LCS)是个经典问题,使用动态规划,实现起来并不难. 一般来说,我们只是输出一个LCS.但是,老师布置的作业是输出所有的LCS. 解法 按照一般的方法,我们首先 ...
- 用Python计算最长公共子序列和最长公共子串
如何用Python计算最长公共子序列和最长公共子串 1. 什么是最长公共子序列?什么是最长公共子串? 1.1. 最长公共子序列(Longest-Common-Subsequences,LCS) 最长公 ...
- python实现查找最长公共子序列
#!/usr/bin/python # -*- coding: UTF-8 -*- worlds = ['fosh','fort','vista','fish','hish','hello','ohd ...
- 【python】Leetcode每日一题-最长公共子序列
[python]Leetcode每日一题-最长公共子序列 [题目描述] 给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度.如果不存在 公共子序列 ,返回 0 . ...
- 用python实现最长公共子序列算法(找到所有最长公共子串)
软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...
- 【ZH奶酪】如何用Python计算最长公共子序列和最长公共子串
1. 什么是最长公共子序列?什么是最长公共子串? 1.1. 最长公共子序列(Longest-Common-Subsequences,LCS) 最长公共子序列(Longest-Common-Subseq ...
- python 回溯法 子集树模板 系列 —— 14、最长公共子序列(LCS)
问题 输入 第1行:字符串A 第2行:字符串B (A,B的长度 <= 1000) 输出 输出最长的子序列,如果有多个,随意输出1个. 输入示例 belong cnblogs 输出示例 blog ...
随机推荐
- ExpressRoute
Extending Your Network to Microsoft Azure Using ExpressRoute https://channel9.msdn.com/events/TechEd ...
- 学习smali
添加控件id 在R$id.smali文件下添加: .field public static final adposition:I = 0x7f05003d 添加类中常量 MainActivity.sm ...
- C# HttpRequest 中文编码问题
工作中的项目要用到别家的网络短信平台,工作中遇到中文编码的问题,特总结以备忘. GET方法: public string DoWebRequest(string url) { ...
- 【转】WinForm不同版本覆盖安装
vs2005为winform程序做的安装包.在以有程序旧版本的机子上用新版本的安装包安装软件时提示 “以经安装该产品的另一个版本.无法继续安装此版本........” 在安装部署项目中设“Remove ...
- MongoDB shell 格式化
直接的方法: db.collection.find().pretty(); 如果想要所有的查询都格式化,可以执行: echo "DBQuery.prototype._prettyShell ...
- 05文件与IO
这节主要学习了read.write.lseek.目录访问(opendir.readdir.closedir)这几个系统调用及其简单的应用. 一旦有了与一个打开文件描述相连的文件描述符,只要该文件是用O ...
- HackerRank "AND xor OR"
Actually I think problem statement is somewhat misleading. No need to mention range [L, R] at all. T ...
- read,for,case,while,if简单例子
Read多用于从某文件中取出每行进行处理 $ cat read.sh #!/bin/bash echo "using read" cat name.txt | while read ...
- golang的的模板引擎之pongo2
https://github.com/flosch/pongo2 beego的扩展包 https://github.com/yansuan/beego-pongo2 gin的扩展包 https://g ...
- 【maven】之开发pom配置常用插件
1.打包跳过测试代码 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId&g ...