Question

After filling the dp table using the standard dynamic programming approach, we can reconstruct one of the Longest Common Subsequences by backtracking. Consider text1 = "ABCBDAB" and text2 = "BDCABA". The dp table is filled as follows:         ""  B   D   C   A   B   A     ""  0   0   0   0   0   0   0     A   0   0   0   0   1   1   1     B   0   1   1   1   1   2   2     C   0   1   1   2   2   2   2     B   0   1   1   2   2   3   3     D   0   1   2   2   2   3   3     A   0   1   2   2   3   3   4     B   0   1   2   2   3   4   4 Using the following Python code snippet for backtracking, what LCS string will be reconstructed? def reconstruct_lcs(text1, text2, dp):     lcs_str = []     i = len(text1)     j = len(text2)     while i > 0 and j > 0:         if text1[i - 1] == text2[j - 1]:             lcs_str.append(text1[i - 1])             i -= 1             j -= 1         elif dp[i - 1][j] > dp[i][j - 1]:             i -= 1         else:             j -= 1     return "".join(lcs_str[::-1]) # Reverse to get correct orde

A "BCBA"
B "BDAB"
C "ABCA"
D "BDBA"
Practice Next

Hey! Ask a query