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 orderSolution
Detailed Answer and Dry Run: The reconstruct_lcs function starts from the bottom-right corner of the dp table (dp[m][n]) and moves upwards and leftwards. If text1[i-1] == text2[j-1], it means these characters form part of the LCS. We append text1[i-1] to our lcs_str and move diagonally up-left (i-1, j-1). If the characters don't match, we look at dp[i-1][j] (value from above) and dp[i][j-1] (value from left). We move to the cell that contributed the maximum value to dp[i][j]. If dp[i-1][j] > dp[i][j-1], we move up (i-1, j). Otherwise, we move left (i, j-1). Let's dry run with text1 = "ABCBDAB" (m=7) and text2 = "BDCABA" (n=6). Initial: i = 7, j = 6, lcs_str = [] 1. i=7, j=6 (dp = 4)   text1[6] = 'B', text2[5] = 'A'. Mismatch.   dp[6][6] (value above) = 3   dp[7][5] (value left) = 4   Since dp[7][5] (4) is not greater than dp[6][6] (3), we move left. (Note: if they are equal, moving left or up is arbitrary, here else moves left).   i = 7, j = 5 2. i=7, j=5 (dp = 4)   text1[6] = 'B', text2[4] = 'B'. Match!   Append 'B' to lcs_str. lcs_str = ['B']   Move diagonally up-left.   i = 6, j = 4 3. i=6, j=4 (dp = 3)   text1[5] = 'A', text2[3] = 'A'. Match!   Append 'A' to lcs_str. lcs_str = ['B', 'A']   Move diagonally up-left.   i = 5, j = 3 4. i=5, j=3 (dp = 2)   text1[4] = 'D', text2[2] = 'C'. Mismatch.   dp[4][3] (value above) = 2   dp[5][2] (value left) = 2   Since dp[4][3] (2) is not greater than dp[5][2] (2), we move left.   i = 5, j = 2 5. i=5, j=2 (dp = 2)   text1[4] = 'D', text2[1] = 'D'. Match!   Append 'D' to lcs_str. lcs_str = ['B', 'A', 'D']   Move diagonally up-left.   i = 4, j = 1 6. i=4, j=1 (dp = 1)   text1[3] = 'B', text2[0] = 'B'. Match!   Append 'B' to lcs_str. lcs_str = ['B', 'A', 'D', 'B']   Move diagonally up-left.   i = 3, j = 0 7. i=3, j=0   j is now 0. The while loop condition j > 0 is false. Loop terminates. Finally, lcs_str = ['B', 'A', 'D', 'B']. We reverse it to get the correct order: ["B", "D", "A", "B"]. The reconstructed LCS is "BDAB".
RBI allows STRIPS trading of Government bonds and certain state government bonds. What does STRIPS stand for?
Which among the following will increase the net worth of an organisation?
Bonds with original maturities of one year or less are called:
Reserve Bank of India (RBI) has been conducting Financial Literacy Week (FLW) every year since 2016. The theme selected for the year is …&hellip...
Which scheme provides collateral-free loans up to ₹5 crores for MSMEs? Â
An investor holds a corporate bond with a Modified Duration of 6.2 years. If the market interest rates suddenly increase by 50 basis points (0.50%), w...
What will be the current yield of a bond with a face value of ₹100 , a coupon interest rate of 10% and market price of ₹80?
Following SEBI's expanded ESG Debt Securities framework effective from June 5, 2025, which of the following is a mandatory operational requirement fo...
Which instrument is used by foreign entities not registered with SEBI to invest in India Market via registered brokers?
A bond that pays compounded interest but the actual cash payment of the bond is deferred till maturity is known as: