'''
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的更多相关文章

  1. 最长公共子序列与最长公共字串 (dp)转载http://blog.csdn.net/u012102306/article/details/53184446

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  2. 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, ...

  3. (字符串)最长公共字串(Longest-Common-SubString,LCS)

    题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...

  4. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  5. poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14113   Accepted: 6260 Descr ...

  6. java_基础知识_字符串练习题_计算两个字符串的最长公共字串长度

    package tek; Java算法——求出两个字符串的最长公共字符串 /** * @Title: 问题:有两个字符串str1和str2,求出两个字符串中最长公共字符串. * @author 匹夫( ...

  7. 【水:最长公共子序列】【HDU1159】【Common Subsequence】

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. 动态规划 ---- 最长公共子序列(Longest Common Subsequence, LCS)

    分析: 完整代码: // 最长公共子序列 #include <stdio.h> #include <algorithm> using namespace std; ; char ...

  9. HDU 1423 最长公共字串+上升子序列

    http://acm.hdu.edu.cn/showproblem.php?pid=1423 在前一道题的基础上多了一次筛选 要选出一个最长的递增数列 lower_bound()函数很好用,二分搜索找 ...

随机推荐

  1. [Codeforces438E][bzoj3625] 小朋友和二叉树 [多项式求逆+多项式开根]

    题面 传送门 思路 首先,我们把这个输入的点的生成函数搞出来: $C=\sum_{i=0}^{lim}s_ix^i$ 其中$lim$为集合里面出现过的最大的数,$s_i$表示大小为$i$的数是否出现过 ...

  2. vs修改快捷键

    https://jingyan.baidu.com/album/9158e0006e10d8a254122826.html?picindex=1 https://sanwen8.cn/p/114IrR ...

  3. HDU RPG的错排 【错排&&组合】

    RPG的错排 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  4. Dinic算法学习&&HDU2063

    http://www.cnblogs.com/SYCstudio/p/7260613.html 看这篇博文懂了一点,做题再体会体会吧 找了好久都没找到一个好用的模板…… 我也是佛了..最后决定用峰神的 ...

  5. PriorityQueue详解(一)

    在Java SE 5.0中,引入了一些新的Collection API,PriorityQueue就是其中的一个.今天由于机缘巧合,花了一个小时看了一下这个类的内部实现,代码很有点意思,所以写下来跟大 ...

  6. 【Tomcat】一台电脑上运行多个tomcat

    效果: 1.首先需要安装Tomcat7,Tomcat8. Tomcat7: Tomcat8: 2.添加两个环境变量,添加CATALINA_HOME1和CATALINA_BASE1指向E:\tomcat ...

  7. 推荐一本书:《UML面向对象建模基础》

    http://www.cnblogs.com/onlytiancai/archive/2006/10/13/528205.html 以前对UML呀,感觉用不上,不知道都干啥的,也就是知道有个用例图.类 ...

  8. python--控制窗体

    窗体的显示和隐藏 #!/usr/bin/env python # -*- coding:utf-8 -*- # author:love_cat import win32con import win32 ...

  9. python爬虫beautifulsoup4系列4-子节点【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/beautifulsoup4/ 前言 很多时候我们无法直接定位到某个元素,我们可以先定位 ...

  10. [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II dp

    4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II Time Limit: 10 Sec  Memory Limit: 128 MBSubmi ...