参考https://oj.leetcode.com/problems/distinct-subsequences

动态规划方程

dp[i][j]=dp[i-1][j-1]+dp[i-1][j] (s(i)==t(i))

dp[i][j]=dp[i-1][j];

边界条件:  iif(j==0) d[i][j]=1;

自己画个矩阵看看。

可能出错,

1.直接递归超时

 public class Solution {
public int numDistinct(String S, String T) {
int len1=S.length();
int len2=T.length();
if(len1<len2) return 0; int ans=dp(S,T,len1,len2);
return ans; }
public int dp(String S,String T,int i,int j)
{
if(i<j) return 0;
if(i==0&&j==0) return 1; // "" ""
if(j==0&&i!=0) return 0;//"xxxx" "" if(S.charAt(i-1)==T.charAt(j-1))
{
return dp(S,T,i-1,j-1)+dp(S,T,i-1,j);
}
else return dp(S,T,i-1,j); }
}

2、加入一个矩阵,依然超时

 public class Solution {
public int numDistinct(String S, String T) {
int len1=S.length();
int len2=T.length();
if(len1<len2) return 0;
int d[][]=new int[len1+1][len2+1]; int ans=dp(S,T,len1,len2,d); return ans; }
public int dp(String S,String T,int i,int j,int d[][])
{
if(i<j) return 0; if(i==0&&j==0) return 1; // "" ""
if(i!=0&&j==0) return 0;
if(d[i][j]!=0) return d[i][j]; if(S.charAt(i-1)==T.charAt(j-1))
{
d[i-1][j-1]=dp(S,T,i-1,j-1,d);
d[i-1][j]=dp(S,T,i-1,j,d);
return d[i-1][j-1]+d[i-1][j];
}
else
{
d[i-1][j]=dp(S,T,i-1,j,d);
return d[i-1][j];
} }
}

3.真正的动态规划

 public class Solution {
public int numDistinct(String S, String T) {
int len1=S.length();
int len2=T.length();
if(len1<len2) return 0;
int d[][]=new int[len1+1][len2+1];
for(int i=0;i<=len1;i++)
{
d[i][0]=1;
}
for(int i=1;i<=len1;i++)
{
for(int j=1;j<=len2&&j<=i;j++)
{
if(S.charAt(i-1)==T.charAt(j-1))
{
d[i][j]=d[i-1][j-1]+d[i-1][j];
}
else
{
d[i][j]=d[i-1][j];
} } } return d[len1][len2]; } }

leetcode distinct-subsequences(DP)的更多相关文章

  1. 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)

    引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...

  2. [LeetCode] Distinct Subsequences 不同的子序列

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

  3. [leetcode]Distinct Subsequences @ Python

    原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...

  4. [LeetCode] Distinct Subsequences 解题思路

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

  5. LeetCode: Distinct Subsequences [115]

    [称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...

  6. LeetCode: Distinct Subsequences 解题报告

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

  7. [LeetCode] Distinct Subsequences [29]

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

  8. [Leetcode] distinct subsequences 不同子序列

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

  9. Distinct Subsequences (dp)

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

  10. Leetcode Distinct Subsequences

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

随机推荐

  1. [Excel] C# ExcelHelper操作类 (转载)

    点击下载 ExcelHelper.rar 主要功能如下1.导出Excel文件,自动返回可下载的文件流 2.导出Excel文件,转换为可读模式3.导出Excel文件,并自定义文件名4.将数据导出至Exc ...

  2. HTML5教程:课时一HTML简介

    一.HTML5新特性 1.HTML5多媒体:标签:视频<video>  :音频<audio> 2.HTML5应用:  本地数据存储:访问本地文件: 本地SQL数据:缓存引用: ...

  3. Android 点击事件,4种回调。

    1.  继承监听接口. 2.  xml方式 : 设置 android:onClick 3. 内部类 4. 匿名类 ------------------------------------------- ...

  4. 【html】【3】html标签列表

    必看参考: http://www.divcss5.com/html/h323.shtml http://www.w3school.com.cn/tags/tag_html.asp 常用: <ht ...

  5. 一个由IsPrime算法引发的细节问题

    //******************************* // //    2014年9月18日星期四,于宿舍撰写 //    作者:夏华林 // //******************* ...

  6. 编写类String的构造函数、拷贝构造函数、析构函数和赋值函数

    一.题目: class String { public: String(const char *str = NULL); // 普通构造函数 String(const String &othe ...

  7. diff函数的实现——LCS的变种问题

    昨天去去哪儿笔试,碰到了一个我们一直很熟悉的命令(diff——ubuntu下面),可以比较字符串,即根据最长公共子串问题,如果A中有B中没有的字符输出形式如下(-ch),如果A中没有,B中有可以输出如 ...

  8. Git 基本使用配置

    // 1.配置用户名邮箱:用于记录你个人的用户名称和电子邮件地址,用户名可随意修改,git 用于记录是谁提交了更新,以及更新人的联系方式: $ git config --global user.nam ...

  9. Win8 +PHP+IIS配置

    1.安装IIS:控制面板-程序和功能-打开或关闭Windows功能 2.配置PHP环境 -添加ISAPI筛选: -添加脚本映射:

  10. go和swift

    你生命中的有些东西终究会失去,比如我住了6年的陈寨,这个聚集了郑州十几万IT民工的地方,说拆就拆了.再比如我玩了3年的坦克英雄,这个带给我太多快乐的游戏,说停就停了. 编程对我而言是种爱好,我上学6年 ...