最长公共子序列(动态规划)
程序员文章站
2022-07-12 08:55:32
...
s1 = [1,3,4,5,6,7,7,8]
s2 = [3,5,7,4,8,6,7,8,2]
d = [[0]*(len(s2)+1) for i in range(len(s1)+1) ]
for i in range(1,len(s1)+1):
for j in range(1,len(s2)+1):
if s1[i-1] == s2[j-1]:
d[i][j] = d[i-1][j-1]+1
else:
d[i][j] = max(d[i-1][j],d[i][j-1])
print ("max LCS number:",d[-1][-1])
上一篇: 股票的最大利润
下一篇: 最长公共子序列动态规划