-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcs.py
More file actions
35 lines (26 loc) · 682 Bytes
/
lcs.py
File metadata and controls
35 lines (26 loc) · 682 Bytes
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
# -*- coding: utf-8 -*-
N = 4
M = 4
STR1 = "abcd"
STR2 = "becd"
memsets = {}
def initialize_memsets():
for i in range(0, 10):
memsets[i] = {}
for j in range(0, 10):
memsets[i][j] = 0
def solve(target_str):
for i in range(0, N):
for j in range(0, M):
if (STR1[i] == STR2[j]):
memsets[i+1][j+1] = memsets[i][j] + 1
target_str += STR1[i]
else:
memsets[i+1][j+1] = max(memsets[i][j+1], memsets[i+1][j])
print memsets[N][M]
print target_str
def __main__():
initialize_memsets()
target_str = ""
solve(target_str)
__main__()