标签:pre 连续 ret 技术 子串 main .com 最长公共子序列 image
区别最长公共子串(连续)
1 ‘‘‘ 2 LCS 最长公共子序列 3 ‘‘‘ 4 5 6 def LCS_len(x, y): 7 m = len(x) 8 n = len(y) 9 dp = [[0] * (n + 1) for i in range(m + 1)] 10 B = [[‘ ‘] * (n + 1) for i in range(m + 1)] 11 for i in range(1, m + 1): 12 for j in range(1, n + 1): 13 if x[i - 1] == y[j - 1]: 14 dp[i][j] = dp[i - 1][j - 1] + 1 15 B[i][j] = ‘X‘ 16 elif dp[i - 1][j] > dp[i][j - 1]: 17 dp[i][j] = dp[i - 1][j] 18 B[i][j] = ‘L‘ 19 else: 20 dp[i][j] = dp[i][j - 1] 21 B[i][j] = ‘T‘ 22 print(dp) 23 return dp, B 24 25 26 def LCS_str(B, x, i, j): 27 if i == 0 or j == 0: 28 return 29 if B[i][j] == ‘X‘: 30 print(x[i - 1]) 31 LCS_str(B, x, i - 1, j - 1) 32 33 elif B[i][j] == ‘L‘: 34 LCS_str(B, x, i - 1, j) 35 else: 36 LCS_str(B, x, i, j - 1) 37 38 39 def main(): 40 x = ‘ABDC‘ 41 y = ‘AFBC‘ 42 dp, B = LCS_len(x, y) 43 LCS_str(B, x, len(x), len(y)) 44 main()
标签:pre 连续 ret 技术 子串 main .com 最长公共子序列 image
原文地址:https://www.cnblogs.com/zle1992/p/8868274.html