资讯专栏INFORMATION COLUMN

[LintCode/LeetCode] Distinct Subsequences [一维DP]

dailybird / 1777人阅读

摘要:用动规方法做建立长度为和的二维数组,表示的第到位子串包含不同的的第到位子串的个数。初始化当的子串长度为时,当的子串长度为时,当和子串都为时,包含,故。

Problem

Given a string S and a string T, count the number of distinct subsequences of T in S.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).

Example

Given S = "rabbbit", T = "rabbit", return 3.

Challenge

Do it in O(n2) time and O(n) memory.

O(n2) memory is also acceptable if you do not know how to optimize memory.

Note

用动规方法做:
建立长度为m+1n+1的二维dp数组,dp[i][j]表示S的第0到i位子串包含不同的T的第0到j位子串的个数。
初始化:当T的子串长度为0时,dp[i][0] = 1;当S的子串长度为0时,dp[0][i] = 0;当S和T子串都为0时,0包含0,故dp[0][0] = 1
两次循环S和T,若S的最后一位和T的最后一位不同,那么S增加一位不会增加更多的包含关系,即dp[i][j]仍然等于dp[i-1][j]。若S的最后一位和T的最后一位相等,最后的结果一定也包含S和T各减一位的结果,如S="abb"T="ab",所以dp[i][j]包含dp[i-1][j-1],再与与dp[i-1][j]相加。

见下面的例子。

     0    A    B    C

0    1    0    0    0

C    1    0    0    0
    
A    1    1    0    0  

B    1    1    1    0   

B    1    1    2    0
 
B    1    1    3    0

C    1    1    3    3
Solution
public class Solution {
    public int numDistinct(String S, String T) {
        int m = S.length(), n = T.length();
        if (m < n) return 0;
        int[][] dp = new int[m+1][n+1];
        for (int i = 0; i <= m; i++) dp[i][0] = 1;
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                dp[i][j] = dp[i-1][j];
                if (S.charAt(i-1) == T.charAt(j-1)) dp[i][j] += dp[i-1][j-1];
            }
        }
        return dp[m][n];
    }
}

One-dimension DP, O(n) space

public class Solution {
    public int numDistinct(String s, String t) {
        int m = s.length();
        int n = t.length();
        int[] dp = new int[n+1];
        dp[0] = 1;
        for (int i = 1; i <= m; i++) {
            for (int j = n; j >= 1; j--) {                   
                if (s.charAt(i-1) == t.charAt(j-1)) {
                    dp[j] += dp[j-1];
                }
            }
        }
        return dp[n];
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65796.html

相关文章

  • [Leetcode] Distinct Subsequences 不同顺序字串

    摘要:计算元素值时,当末尾字母一样,实际上是左方数字加左上方数字,当不一样时,就是左方的数字。示意图代码如果这个字符串有个怎么办用暴力法,对每一位开始向后检查是否是。 Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of T in S. A su...

    SnaiLiu 评论0 收藏0
  • [LintCode/LeetCode] Unique Paths

    摘要:简单的动规题目,建立数组。坐标为矩阵的坐标,值为从左上角到这一格的走法总数。赋初值,最上一行和最左列的所有格子的走法都只有一种,其余格子的走法等于其左边格子走法与上方格子走法之和。最后,返回即可。 Problem A robot is located at the top-left corner of a m x n grid (marked Start in the diagram ...

    Gu_Yan 评论0 收藏0
  • [LintCode/LeetCode] Unique Paths II

    摘要:和完全一样的做法,只要在初始化首行和首列遇到时置零且即可。对了,数组其它元素遇到也要置零喏,不过就不要啦。 Problem Follow up for Unique Paths: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle...

    firim 评论0 收藏0
  • leetcode115. Distinct Subsequences

    摘要:题目要求判断字符串中通过删减单词含有几个字符串。例如中含有个字符串,通过分别删除第,,个。也就是说,我们需要通过一个数据结构来记录临时结果从而支持我们在已知前面几个情况的场景下对后续情况进行计算。 题目要求 Given a string S and a string T, count the number of distinct subsequences of S which equa...

    NSFish 评论0 收藏0
  • Distinct Subsequences

    摘要:终于见到一个使用动态规划的题目了,似乎这种字符串比对的差不多都是的思路。从后向前递推,我们可以得到下面的矩阵可以看出,矩阵中每个的数值为,这样右下角的值即为所求。 Problem Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of...

    Ajian 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<