-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlcsnew.py
More file actions
23 lines (22 loc) · 785 Bytes
/
Copy pathlcsnew.py
File metadata and controls
23 lines (22 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
cache = {}
def lcs(a, b):
try:
return cache[tuple(sorted((tuple(a), tuple(b))))]
except KeyError:
if not (len(a) and len(b)):
cache[tuple(sorted((tuple(a), tuple(b))))] = []
return []
if a[-1] == b[-1]:
result = lcs(a[:-1], b[:-1]) + [a[-1]]
cache[tuple(sorted((tuple(a), tuple(b))))] = result
return result
first = lcs(a, b[:-1])
second = lcs(a[:-1], b)
if len(first) > len(second):
cache[tuple(sorted((tuple(a), tuple(b))))] = first
return first
cache[tuple(sorted((tuple(a), tuple(b))))] = second
return second
raw_input()
a,b = map(int, raw_input().split()),map(int, raw_input().split())
print ' '.join(map(str, lcs(a, b)))