'''
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. BZOJ4552 [Tjoi2016&Heoi2016]排序 【二分 + 线段树】

    题目链接 BZOJ4552 题解 之前去雅礼培训做过一道题,\(O(nlogn)\)维护区间排序并能在线查询 可惜我至今不能get 但这道题有着\(O(nlog^2n)\)的离线算法 我们看到询问只有 ...

  2. 【CZY选讲·次大公因数】

    题目描述 给定n个数ai,求sgcd(a1,a1),sgcd(a1,a2),…,sgcd(a1,an). 其中sgcd(x,y)表示x和y的次大公因数.若不存在次大公因数,sgcd(x,y)=-1 ...

  3. js实现封装和继承

    封装(模拟对象) http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html 继承 htt ...

  4. Debian中文字体安装

    默认装的英文办的debian7,看中国字不太美,这好办照着做吧 1. Setup locales #dpkg-reconfigure locales 选择 zh_CN GB2312 zh_CN.GBK ...

  5. Spring 的IOC 和Aop

    Spring 的IOC 和Aop

  6. 同余方程(NOIP2012)

    原题传送门 水~ 纯拓展欧几里得算法.. #include<iostream> #include<cstdio> #define ll long long using name ...

  7. Linux内核情景分析之异常访问,用户堆栈的扩展

    情景假设: 在堆内存中申请了一块内存,然后释放掉该内存,然后再去访问这块内存.也就是所说的野指针访问. 当cpu产生页面错误时,会把失败的线性地址放在cr2寄存器.线性地址缺页异常的4种情况 1.如果 ...

  8. 使用aiohttp的一个小例子

    #!/usr/bin/env python # encoding: utf-8 import aiohttp import asyncio import traceback import time i ...

  9. hdu 5146(水题)

    Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  10. poj 2546(两圆公共面积)

    Circular Area Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5682   Accepted: 2225 Des ...