Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,18 @@
|
|||
def lcs(a, b):
|
||||
# generate matrix of length of longest common subsequence for substrings of both words
|
||||
lengths = [[0] * (len(b)+1) for _ in range(len(a)+1)]
|
||||
for i, x in enumerate(a):
|
||||
for j, y in enumerate(b):
|
||||
if x == y:
|
||||
lengths[i+1][j+1] = lengths[i][j] + 1
|
||||
else:
|
||||
lengths[i+1][j+1] = max(lengths[i+1][j], lengths[i][j+1])
|
||||
|
||||
# read a substring from the matrix
|
||||
result = ''
|
||||
j = len(b)
|
||||
for i in range(1, len(a)+1):
|
||||
if lengths[i][j] != lengths[i-1][j]:
|
||||
result += a[i-1]
|
||||
|
||||
return result
|
||||
Loading…
Add table
Add a link
Reference in a new issue