最长公共字串算法, 文本比较算法, longest common subsequence(LCS) algorithm
'''
merge two configure files, basic file is aFile
insert the added content of bFile compare to aFile
for example, 'bbb' is added content
-----------------------------------------------------------
a file content | b file content | c merged file content
111 | 111 | 111
aaa | bbb | aaa
| | bbb
222 | 222 | 222
------------------------------------------------------------
'''
def mergeFiles(aPath, bPath, cPath): with open(aPath, 'r') as f:
aLines = f.readlines();
aLines = [ line.strip() + '\n' for line in aLines] with open(bPath, 'r') as f:
bLines = f.readlines();
bLines = [ line.strip() + '\n' for line in bLines] cLines = mergeSequences(aLines, bLines) with open(cPath, 'w') as f:
for line in cLines:
f.write(line) '''
merge the sequence
'''
def mergeSequences(aLines, bLines):
record = {}
lcs = findLCS(record, aLines, 0, bLines, 0)
currA = currB = 0
merged = []
for (line, aI, bI) in lcs: # add deleted
if aI > currA:
merged.extend(aLines[currA:aI])
currA = aI + 1 # add added
if bI > currB:
merged.extend(bLines[currB:bI])
currB = bI + 1 # add common
merged.append(line) if currA < len(aLines):
merged.extend(aLines[currA:])
if currB < len(bLines):
merged.extend(bLines[currB:]) return merged '''
find Longest common subsequence
return list of (line, x, y)
line is common line, x is the index in aLines, y is the index in bLines
TODO: eliminate recursive invoke, use dynamic algorithm
'''
def findLCS(record, aLines, aStart, bLines, bStart): key = lcsKey(aStart, bStart)
if record.has_key(key):
return record[key] aL = aLines[aStart:]
bL = bLines[bStart:]
if len(aL) > 0 and len(bL) > 0:
if aL[0] == bL[0]:
lsc = [(aL[0], aStart, bStart)]
lsc.extend(findLCS(record, aLines, aStart + 1, bLines, bStart + 1))
record[key] = lsc
return lsc
else:
aLsc = findLCS(record, aLines, aStart, bLines, bStart + 1)
bLsc = findLCS(record, aLines, aStart + 1, bLines, bStart) if len(aLsc) > len(bLsc):
record[key] = aLsc
return aLsc
else:
record[key] = bLsc
return bLsc
else:
return [] Code
最长公共字串算法, 文本比较算法, longest common subsequence(LCS) algorithm的更多相关文章
- 最长公共子序列与最长公共字串 (dp)转载http://blog.csdn.net/u012102306/article/details/53184446
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- URAL 1517 Freedom of Choice(后缀数组,最长公共字串)
题目 输出最长公共字串 #define maxn 200010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int cmp(int *r,int a,int b, ...
- (字符串)最长公共字串(Longest-Common-SubString,LCS)
题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14113 Accepted: 6260 Descr ...
- java_基础知识_字符串练习题_计算两个字符串的最长公共字串长度
package tek; Java算法——求出两个字符串的最长公共字符串 /** * @Title: 问题:有两个字符串str1和str2,求出两个字符串中最长公共字符串. * @author 匹夫( ...
- 【水:最长公共子序列】【HDU1159】【Common Subsequence】
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 动态规划 ---- 最长公共子序列(Longest Common Subsequence, LCS)
分析: 完整代码: // 最长公共子序列 #include <stdio.h> #include <algorithm> using namespace std; ; char ...
- HDU 1423 最长公共字串+上升子序列
http://acm.hdu.edu.cn/showproblem.php?pid=1423 在前一道题的基础上多了一次筛选 要选出一个最长的递增数列 lower_bound()函数很好用,二分搜索找 ...
随机推荐
- gulp实时刷新页面
需要安装nodejs 全局安装gulp cnpm install -g gulp 局部安装 cnpm install -save-dev gulp 添加配置文件,新建gulpfile.js var g ...
- sql优化 in 和 not in 语句
WHY? IN 和 NOT IN 是比较常用的关键字,为什么要尽量避免呢? 1.效率低 可以参看我之前遇到的一个例子([小问题笔记(九)] SQL语句Not IN 效率低,用 NOT EXISTS试试 ...
- 关于PDA、GPS等动态资源的几种GIS解决方案
关于PDA.GPS等动态资源的几种GIS解决方案(原创) 今年来GIS发展迅速,特别是实时监控中引入了GPS,PDA等动态资源,使得GIS在各个行业的应用更为广泛. 1.在这些动态资源资源的监控中主要 ...
- 莫队算法初识~~CodeForces - 617E
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- c++ 操作防火墙
原文转自 https://msdn.microsoft.com/zh-cn/library/aa364726(v=vs.85).aspx #include <windows.h> #inc ...
- UVA 10334 Ray Through Glasses
自己手动画了第三项发现f[3]=5;就猜斐波那契了.实际上光线分为两种距离外界有2面玻璃,1面玻璃 其分别时n-1次反射,n-2次反射形成的 故推出斐波那契. 手动一些f1,f2,f3就OK #inc ...
- AC日记——Count on a tree II spoj
Count on a tree II 思路: 树上莫队: 先分块,然后,就好办了: 来,上代码: #include <cmath> #include <cstdio> #inc ...
- Python与数据结构[1] -> 栈/Stack[1] -> 中缀表达式与后缀表达式的转换和计算
中缀表达式与后缀表达式的转换和计算 目录 中缀表达式转换为后缀表达式 后缀表达式的计算 1 中缀表达式转换为后缀表达式 中缀表达式转换为后缀表达式的实现方式为: 依次获取中缀表达式的元素, 若元素为操 ...
- #422 Div2 D
#422 Div2 D 题意 假设有 n 个人比赛,每次比赛进行分组,每组人数必须相同,如果一组有 x 人,则那一组要比赛 $ \frac{x * (x - 1)}{2}$次,最终一人获胜,其它人淘汰 ...
- 使用CXF开发RESTFul服务
相信大家在阅读CXF官方文档(http://cxf.apache.org/docs/index.html)时,总是一知半解.这里向大家推荐一本PacktPub.Apache.CXF.Web.Servi ...