You are viewing a single comment's thread. Return to all comments →
def leastCommonSequence(seq1, seq2): lcs = [[[] for c in range(len(seq1)+1)]for y in range(len(seq2)+1)] for i in range(1, len(seq2)+1): for j in range(1, len(seq1)+1): if seq1[j-1] == seq2[i -1]: lcs[i][j] = lcs[i-1][j-1] + [seq2[i-1]] else: lcs[i][j] = max(lcs[i-1][j], lcs[i][j-1], key=len) return lcs[-1][-1] n, m = map(int, input().split(' ')) a = list(map(int, input().split(' '))) b = list(map(int, input().split(' '))) result = leastCommonSequence(a, b) for i in result: print(i, end = " ")
Seems like cookies are disabled on this browser, please enable them to open this website
The Longest Common Subsequence (LCS)
You are viewing a single comment's thread. Return to all comments →