最长公共字串算法, 文本比较算法, longest common subsequence(LCS) algorithm
2024-10-21 11:53:45
'''
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
最新文章
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(34)-文章发布系统①-简要分析
- 解决:Redis:java.util.NoSuchElementException: Unable to validate object at
- “Transaction rolled back because it has been marked as rollback-only”
- ActiveReports 报表应用教程 (3)---图表报表
- pip 直接安装tar.gz zip文件包 (windows linux mac 可用)
- php 注入
- java多线程之:创建开启一个线程的开销
- js 跨域的问题 (同一个主域名不同的二级域名下的跨域问题) 解决 WdatePicker.js my97日期选择控件
- 判断IE版本的语句 [if lte IE 6]...[endif]
- Poj2948Martian Mining(记忆化)
- 批处理:循环解压不同文件夹下的zip压缩包
- webwervice发布时出错 java.security.PrivilegedActionException
- pdf点击超链接后返回:alt+ 向左 /向右
- cmake 入门实战
- mmap:速度快+整块操作
- Ubuntu和Windows双系统的安装
- Linux su切换用户后命令提示符变为bash-4.2$
- Windows Community Toolkit 3.0 - InfiniteCanvas
- bzoj 1531 Bank notes 多重背包/单调队列
- 剪格子|2013年蓝桥杯A组题解析第九题-fishers