-
Notifications
You must be signed in to change notification settings - Fork 88
/
lcs.py
53 lines (47 loc) · 1.45 KB
/
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
''' mbinary
#########################################################################
# File : lcs.py
# Author: mbinary
# Mail: [email protected]
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2018-08-25 12:00
# Description:
#########################################################################
'''
def lcs(a, b):
'''time: O(mn); space: O(mn)'''
m, n = len(a), len(b)
board = [[[] for i in range(n+1)] for i in range(m+1)]
for i in range(m):
for j in range(n):
if a[i] == b[j]:
board[i+1][j+1] = board[i][j]+[a[i]]
elif len(board[i][j+1]) < len(board[i+1][j]):
board[i+1][j+1] = board[i+1][j]
else:
board[i+1][j+1] = board[i][1+j]
return board[m][n]
def lcs2(a, b):
'''time: O(mn); space: O(m)'''
m, n = len(a), len(b)
board = [[] for i in range(n+1)]
for i in range(m):
upperLevel = board[0].copy()
for j in range(n):
tmp = board[j+1].copy()
if a[i] == b[j]:
board[j+1] = upperLevel+[a[i]]
elif len(board[j+1]) < len(board[j]):
board[j+1] = board[j].copy() # copy is needed
upperLevel = tmp
return board[n]
if __name__ == '__main__':
a = 'ABCBDAB'
b = 'BDCABA'
print('s1:', a)
print('s2:', b)
while 1:
print('lcs:', lcs2(a, b))
a = input('s1: ')
b = input('s2: ')