-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCS.py
24 lines (20 loc) · 823 Bytes
/
LCS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 最长公共子序列
def longestCommonSubsequence(source, dest): # 输入值, 要比较的值
inLen = len(source)
outLen = len(dest)
target = []
cell = [[0 for j in range(inLen + 1)] for i in range(outLen + 1)]
for i in range(1, outLen + 1):
for j in range(1, inLen + 1):
if dest[i - 1] == source[j - 1]:
cell[i][j] = cell[i - 1][j - 1] + 1
target.append(dest[i - 1])
else:
cell[i][j] = max(cell[i - 1][j], cell[i][j - 1])
for i in range(1, outLen + 1):
for j in range(1, inLen + 1):
print(cell[i][j], end=', ')
print()
print("The Longest sub sequence of \"%s\" and \"%s\" is %s" % (source, dest, target))
if __name__ == "__main__":
longestCommonSubsequence("fish", "fosh")