码迷,mamicode.com
首页 > 其他好文 > 详细

115 Distinct Subsequences

时间:2015-07-21 06:48:13      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

115 Distinct Subsequences

这道题是dp, 我定义的dp[i][j] 为用t[:i] 和s[:j] 的disitinct subsequence排列数

class Solution:
    # @param {string} s
    # @param {string} t
    # @return {integer}
    def numDistinct(self, s, t):
        m, n = len(t), len(s)
        if m == 0 or n == 0:
            return 0
        dp,cur = [0] * n, 1
        for i in range(0, m):
            for j in range(0, n):
                tmp = dp[j]
                if t[i] == s[j]:
                    dp[j] = cur
                else:
                    dp[j] = 0
                cur += tmp
            cur = 0
        return sum(dp)

 

115 Distinct Subsequences

标签:

原文地址:http://www.cnblogs.com/dapanshe/p/4663261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!