-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractical-12.py
executable file
·36 lines (28 loc) · 1012 Bytes
/
practical-12.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
{
"Author": "Fenil Gandhi",
"Version": "Python 3.6.2",
"Description": "Implement LCS problem",
"Input Parameters": "2 String Sequences",
"Input": ["abcef", "pqrabcdef"],
"Output": "The Longest Common Subsequence between 'abcef' and 'pqrabcdef' is abcef of 5 characters",
}
def LCS(x, y):
subsequence = ''
if len(x) > len(y):
x, y = y, x
solution = [0] + [0] * len(x)
prev = solution
if x[0] == y[0]:
subsequence += y[0]
for j in range(len(y)):
for i in range(1, len(x) + 1):
if (x[i - 1] == y[j]):
solution[i] = max(0, prev[i - 1] + 1)
else:
solution[i] = max(solution[i - 1], solution[i])
if solution != prev:
subsequence += y[j]
prev = solution[:]
print("The Longest Common Subsequence between \"{0}\" and \"{1}\" is \"{2}\" of {3} characters".format(x, y, subsequence, solution[-1]))
if __name__ == '__main__':
LCS("abcef", "pqrabcdef")