-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcs.py
48 lines (41 loc) · 983 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import utils
# longest common subsequence
# let C[i,j] = |LCS(x[1..i], LCS(y[1..j]))
# C[i,j] = C[i-1,j-1] + 1 if x[i]==y[j] # take i,j - can only make LCS longer
# max(C[i,j-1], C[i-1,j]) # advance one of them
def LCS(x,y):
m=len(x)
n=len(y)
C=matrix(m+1,n+1)
P=matrix(m+1,n+1)
for i in range(1,m+1):
for j in range(1,n+1):
if (x[i-1]==y[j-1]):
C[i][j]=C[i-1][j-1]+1
P[i][j]=1 # mark it
else:
C[i][j]=max(C[i][j-1],C[i-1][j])
# reconstruct
S=[]
i=m
j=n
# for l in C: print l
# print "---------------------------"
# for l in P: print l
while(i>0 and j>0):
if (P[i][j]==1):
assert (x[i-1] == y[j-1])
S.append(x[i-1])
i=i-1
j=j-1
else:
if (C[i-1][j]>=C[i][j-1]):
i=i-1
else:
j=j-1
S.reverse()
return S
# exercise: LCS with space O(min(n,m))
# problem 15-2: longest palindrome subsequence
# every common subsequence between S, reversed(S) is a palindrome subsequence of S
# eg ChARACter, retCARAhC