第九章 动态规划part15

392. 判断子序列

class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        n1 = len(t)
        n2 = len(s)
        dp = [[0] * (n1+1) for i in range(n2+1)]
        for i in range(1,n2+1):
            for j in range(1,n1+1):
                if s[i-1] == t[j-1]:
                    dp[i][j] = dp[i-1][j-1] + 1
                else:
                    dp[i][j] = max(dp[i-1][j-1],dp[i-1][j],dp[i][j-1])
        return(dp[n2][n1] == n2)

115. 不同的子序列

class Solution:
    def numDistinct(self, s: str, t: str) -> int:
        n1 = len(t)
        n2 = len(s)
        dp = [[0] * (n1+1) for i in range(n2+1)]
        for i in range(n2+1):
            dp[i][0] = 1
        for i in range(1,n2+1):
            for j in range(1,n1+1):
                if s[i-1] == t[j-1]:
                    dp[i][j] = dp[i-1][j-1] + dp[i-1][j]
                else:
                    dp[i][j] = dp[i-1][j]
        return(dp[n2][n1])

上一篇:【切换网络连接后】VMware虚拟机网络配置【局域网通信】


下一篇:keil创建单片机工程