forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest common subsequence.py
48 lines (44 loc) · 1.34 KB
/
longest common subsequence.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
"""
LCS Problem Statement: Given two sequences, find the length of longest subsequence present in both of them.
A subsequence is a sequence that appears in the same relative order, but not necessarily continious.
Example:"abc", "abg" are subsequences of "abcdefgh".
"""
def LCS(x,y):
b=[[] for j in range(len(x)+1)]
c=[[] for i in range(len(x))]
for i in range(len(x)+1):
b[i].append(0)
for i in range(1,len(y)+1):
b[0].append(0)
for i in range(len(x)):
for j in range(len(y)):
if x[i]==y[j]:
b[i+1].append(b[i][j]+1)
c[i].append('/')
elif b[i][j+1]>=b[i+1][j]:
b[i+1].append(b[i][j+1])
c[i].append('|')
else :
b[i+1].append(b[i+1][j])
c[i].append('-')
return b,c
def print_lcs(x,c,n,m):
n,m=n-1,m-1
ans=[]
while n>=0 and m>=0:
if c[n][m]=='/':
ans.append(x[n])
n,m=n-1,m-1
elif c[n][m]=='|':
n=n-1
else:
m=m-1
ans=ans[::-1]
return ans
if __name__=='__main__':
x=['a','b','c','b','d','a','b']
y=['b','d','c','a','b','a']
b,c=LCS(x,y)
print('Given \nX : ',x)
print('Y : ',y)
print('LCS : ',print_lcs(x,c,len(x),len(y)))