-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4 - LongestCommonSubsequence.py
56 lines (41 loc) · 1.13 KB
/
4 - LongestCommonSubsequence.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
54
def longestCommonSubsequence(i, j):
global a,b,n,m
if i>=n or j>=m:
return 0
if (i,j) in memory:
return memory[i,j]
if a[i] == b[j]:
memory[i,j] = 1 + longestCommonSubsequence(i+1,j+1)
backtrack[i,j] = 1,1
return memory[i,j]
left = longestCommonSubsequence(i+1,j)
right = longestCommonSubsequence(i, j+1)
if left>=right:
chosen = left
backtrack[i,j] = 1,0
elif right>left:
chosen = right
backtrack[i,j] = 0,1
memory[i,j] = chosen
return memory[i,j]
def printer():
i = 0
j = 0
while(1):
if i+1>n or j+1>m:
break
if a[i] == b[j]:
print(a[i], end = ' ')
if (i,j) in backtrack:
myop = backtrack[i,j]
i = i + int(myop[0])
j = j + int(myop[1])
else:
break
n, m = map(int, input().split())
a = list(map(int, input().rstrip().split()))
b = list(map(int, input().rstrip().split()))
memory = {}
backtrack = {}
length = longestCommonSubsequence(0, 0)
printer()