最长公共子序列 & 最长公共子串的区别:

找两个字符串的最长公共子串,这个子串要求在原字符串中是连续的。而最长公共子序列则并不要求连续。

leetcode 1143题 最长公共子序列

!!!字符可以不连续
给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列。

一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
例如,“ace” 是 “abcde” 的子序列,但 “aec” 不是 “abcde” 的子序列。两个字符串的「公共子序列」是这两个字符串所共同拥有的子序列。
若这两个字符串没有公共子序列,则返回 0。

示例 1:
输入:text1 = “abcde”, text2 = “ace”
输出:3
解释:最长公共子序列是 “ace”,它的长度为 3。
示例 2:
输入:text1 = “abc”, text2 = “abc”
输出:3
解释:最长公共子序列是 “abc”,它的长度为 3。
示例 3:
输入:text1 = “abc”, text2 = “def”
输出:0
解释:两个字符串没有公共子序列,返回 0。
提示:
1 <= text1.length <= 1000
1 <= text2.length <= 1000
输入的字符串只含有小写英文字符。

# leetcode submit region begin(Prohibit modification and deletion)

class Solution(object):
    def longestCommonSubsequence(self, text1, text2):
        """
        :type text1: str
        :type text2: str
        :rtype: int
        """
        t1,t2 = len(text1),len(text2)
        dp = [[0 for j in range(t2+1)] for i in range(t1+1)]
        # 0行和0列都是字符串和空字符比较,为0
        for i in range(1,t1+1):
            for j in range(1,t2+1):
                # 如果匹配 则移除i,j对应的字符,结果+1 
                # 然后再去找之前的最大值
                if text1[i-1]==text2[j-1]:
                    dp[i][j] = 1 + dp[i-1][j-1]
                else:
                # 如果不匹配,则为0, 去找左边或者右边最大值
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1])
            #print(dp[i][:])
        return(dp[-1][-1])

# leetcode submit region end(Prohibit modification and deletion)

字符串a,b中的最长公共子串

题目描述
查找两个字符串a,b中的最长公共子串。若有多个,输出在较短串中最先出现的那个。
输入描述:
输入两个字符串
输出描述:
返回重复出现的字符

while True:
    try:
        str1,str2 = input(),input()
        # 短的为text1 长的为text2
        text1, text2 = (str1, str2) if len(str1) < len(str2) else (str2, str1)
        t1,t2 = len(text1),len(text2)
        dp = [[0 for j in range(t2+1)] for i in range(t1+1)]
        
        maxlen = 0
        for i in range(1,t1+1):
            for j in range(1,t2+1):
                # 如果匹配 则移除i,j对应的字符,去找之前的最大值
                if text1[i-1]==text2[j-1]:
                    dp[i][j] = 1 + dp[i-1][j-1]
                    if dp[i][j]>maxlen:
                        # 记录最长公共子串的长度
                        maxlen = dp[i][j]
                        start = i - maxlen  
                # 如果不匹配,则为0
        print(text1[start:start+maxlen])
    except:
        break

最长上升子序列

给定一个无序的整数数组,找到其中最长上升子序列的长度。

示例:
输入: [10,9,2,5,3,7,101,18]
输出: 4
解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。
说明: 可能会有多种最长上升子序列的组合,你只需要输出对应的长度即可。

#  进阶: 你能将算法的时间复杂度降低到 O(n log n) 吗? 
#  Related Topics 二分查找 动态规划
# leetcode submit region begin(Prohibit modification and deletion)
class Solution(object):
    def lengthOfLIS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) == 0:
            return 0
        n = len(nums)
        lengths = [1 for _ in range(n)]

        for i in range(1,n):
            for j in range(i):
                # 坐标锁定在i, 然后遍历j(从0->i-1)
                if nums[j] < nums[i]:
                    lengths[i] = max(lengths[j] + 1,lengths[i])

            #print(lengths)
        return(max(lengths))
# leetcode submit region end(Prohibit modification and deletion)