-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestCommonSubsequence.py
70 lines (59 loc) · 1.92 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'''
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 contiguous.
For example, “abc”, “abg”, “bdf”, “aeg”, ‘”acefg”, .. etc are subsequences of “abcdefg”.
So a string of length n has 2^n different possible subsequences.
A Dynamic Programming Problem
Input Format
-------------
ABCDGH
AEDFHR
-------------
LCS for input Sequences “ABCDGH” and “AEDFHR” is “ADH” of length 3.
another:
LCS for input Sequences “AGGTAB” and “GXTXAYB” is “GTAB” of length 4.
Algorithm Description:
Start from the end for these sequence preservation
Haha easy pesy.I just need to think more properly
'''
s1 = input()
s2 = input()
# This approach is good but non memoized method
def solve(s1, s2):
if len(s1) <1 or len(s2) <1:
return 0
if len(s1) == 1 and s2[-1] == s1[-1]:
return 1
if len(s2) == 1 and s1[-1] == s2[-1]:
return 1
if s1[-1] == s2[-1]:
return 1 + solve(s1[:len(s1)-1], s2[:len(s2)-1])
else:
return max(solve(s1[:len(s1)-1], s2), solve(s1, s2[:len(s2)-1]))
# print(solve(s1,s2))
# My favourite method if memoization is to directly record the function parameters
# in a map. Watch and learn
memory = {}
def Dynamic_solve(s1,s2):
if len(s1) <1 or len(s2) <1:
memory[(s1,s2)] = 0
return 0
if len(s1) == 1 and s2[-1] == s1[-1]:
memory[(s1,s2)] = 1
return memory[(s1,s2)]
if len(s2) == 1 and s1[-1] == s2[-1]:
memory[(s1,s2)] = 1
return memory[(s1,s2)]
if s1[-1] == s2[-1]:
try:
return 1+ memory[(s1[:len(s1)-1], s2[:len(s2)-1])]
except:
memory[(s1,s2)] = 1 + solve(s1[:len(s1)-1], s2[:len(s2)-1])
return memory[(s1,s2)]
else:
try:
return max(memory[(s1[:len(s1)-1],s2)] , memory[(s1, s2[:len(s2)-1])])
except:
memory[(s1,s2)] = max(solve(s1[:len(s1)-1], s2), solve(s1, s2[:len(s2)-1]))
return memory[(s1,s2)]
print(Dynamic_solve(s1,s2))