最长公共子序列问题python源代码
时间: 2023-09-24 19:03:50 浏览: 71
最长公共子序列问题-源代码
def lcs(x, y):
m = len(x)
n = len(y)
c = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if x[i - 1] == y[j - 1]:
c[i][j] = c[i - 1][j - 1] + 1
else:
c[i][j] = max(c[i][j - 1], c[i - 1][j])
return c[m][n]
阅读全文