LintCode77:Longest Common Subsequence

Description

Given two strings, find the longest common subsequence (LCS).

Your code should return the length of LCS.

Have you met this question in a real interview?  Yes

Problem Correction

Clarification

What's the definition of Longest Common Subsequence?

  • https://en.wikipedia.org/wiki/Longest_common_subsequence_problem
  • http://baike.baidu.com/view/2020307.htm

Example

For "ABCD" and "EDCA", the LCS is "A" (or "D""C"), return 1.

For "ABCD" and "EACB", the LCS is "AC", return 2.


lintcode:链接

最长公共子串:链接

参考链接:动态规划

最长公共子序列是一个十分实用的问题,可以不连续它可以描述两段文字之间的“相似度”,即它们的雷同程度,从而能够用来辨别抄袭。对一段文字进行修改之后,计算改动前后文字的最长公共子序列,将除此子序列外的部分提取出来,这种方法判断修改的部分,往往十分准确。

dp数组为(m+1)*(n+1),dp[i][j]代表的是A数组前i个数(不包括i)和B数组前j个数(不包括j)的最长公共子序列

如果两个字符相等,则dp值是二者都往前回退一个字符的dp+1;如果不相等,dp值就是其中回退一个字符的最大值。 该时间复杂度是O(mn),空间复杂度是O(mn),空间复杂度可以进一步优化,见上方参考链接。

class Solution:
    """
    @param A: A string
    @param B: A string
    @return: The length of longest common subsequence of A and B
    """
    def longestCommonSubsequence(self, A, B):
        # write your code here
        m, n = len(A), len(B)
        # 这样做的目的是避开0的位置 
        # dp[i][j]代表的是A数组前i个数(不包括i)和B数组前j个数(不包括j)的最长公共子序列
        dp = [[0] * (n+1) for i in range(m+1)]
        for i in range(1, m+1):
            for j in range(1, n+1):
                if A[i-1] == B[j-1]:
                    dp[i][j] = dp[i-1][j-1] + 1
                else:
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1])
        # return dp[m][n]
        return dp

    def print_LCS(self, A, B):
        dp = self.longestCommonSubsequence(A, B)
        i, j = len(A), len(B)
        LCS = ""
        while i > 0 and j > 0:
            if A[i-1] == B[j-1] and dp[i][j] == dp[i-1][j-1] + 1:
                LCS = A[i-1] + LCS
                i -= 1
                j -= 1 
                continue
            if dp[i][j] == dp[i-1][j]:
                i -= 1
                continue
            if dp[i][j] == dp[i][j-1]:
                j -= 1
                continue
        return LCS

s = Solution()
print(s.longestCommonSubsequence([1,2],[1,4,2,3]))
print(s.print_LCS('ABCD','EACB'))

 

你可能感兴趣的:(LeetCode)