-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path(Algorithm)Common_Child(LCS).py
More file actions
34 lines (31 loc) · 1.11 KB
/
Copy path(Algorithm)Common_Child(LCS).py
File metadata and controls
34 lines (31 loc) · 1.11 KB
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
'''
Algorithm: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem
1. Need not use a real 2-d matrix, just use two list to replace the 2-d matrix save a lot time.
2. Use if-else statement instead of max() operation to save time.
3. Use set operation to eliminate the different elements which do not exit in s1 and s2 at the same time.
'''
#!/bin/python3
import sys
def commonChild(s1, s2):
common = set(s1) & set(s2)
s1 = ''.join([c for c in s1 if c in common])
s2 = ''.join([c for c in s2 if c in common])
# generate a two lists
previous = [0 for i in range(len(s1) + 1)]
current = previous[::]
#counting
for i in range(1, len(s2) + 1):
for j in range(1, len(s1) + 1):
if s2[i - 1] != s1[j - 1]:
if previous[j] > current[j - 1]:
current[j] = previous[j]
else:
current[j] = current[j - 1]
else:
current[j] = previous[j - 1] + 1
previous = current[::]
return current[-1]
s1 = input().strip()
s2 = input().strip()
result = commonChild(s1, s2)
print(result)