-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlongestCommonSuperSequence.py
99 lines (81 loc) · 2.55 KB
/
longestCommonSuperSequence.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def lcs(a, b, x, y):
dp = [[None for _ in range(y + 1)] for _ in range(x + 1)]
for i in range(x + 1):
for j in range(y + 1):
if i == 0 or j == 0:
dp[i][j] = 0
elif a[i - 1] == b[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[x][y]
def LongestCommonSequenceRec(a, b, x, y):
if x == 0:
return y
if y == 0:
return x
if a[x - 1] == b[y - 1]:
return 1 + LongestCommonSequenceRec(a, b, x - 1, y - 1)
else:
return 1 + min(
LongestCommonSequenceRec(a, b, x - 1, y),
LongestCommonSequenceRec(a, b, x, y - 1),
)
def LongestCommonSequenceDp(a, b, x, y):
dp = [[None for _ in range(y + 1)] for _ in range(x + 1)]
for i in range(x + 1):
for j in range(y + 1):
if i == 0:
dp[i][j] = j
elif j == 0:
dp[i][j] = i
elif a[i - 1] == b[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1])
return dp[x][y]
def LongestCommonSequence(a, b, x, y):
return x + y - lcs(a, b, x, y)
def PrintLongestCommonSequence(a, b, x, y):
dp = [[None for _ in range(y + 1)] for _ in range(x + 1)]
for i in range(x + 1):
for j in range(y + 1):
if i == 0:
dp[i][j] = j
elif j == 0:
dp[i][j] = i
elif a[i - 1] == b[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1])
i = x
j = y
string = []
while i > 0 and j > 0:
if a[i - 1] == b[j - 1]:
string.append(a[i - 1])
i -= 1
j -= 1
elif dp[i - 1][j] > dp[i][j - 1]:
string.append(b[j - 1])
j -= 1
else:
string.append(a[i - 1])
i -= 1
while i > 0:
string.append(a[i - 1])
i -= 1
while j > 0:
string.append(b[j - 1])
j -= 1
string.reverse()
return "".join(string)
if __name__ == "__main__":
a = input()
b = input()
x = len(a)
y = len(b)
print(LongestCommonSequenceRec(a, b, x, y))
print(LongestCommonSequenceDp(a, b, x, y))
print(LongestCommonSequence(a, b, x, y))
print(PrintLongestCommonSequence(a, b, x, y))