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).

Here is an example:
S = "rabbbit", T = "rabbit"

Return 3.

题意解读:只可以用删除字符的方法从第一个字符串变换到第二个字符串,求出一共有多少种变换方法。
解题分析:dfs可以做,但大数据超时。
动态规划,定义dp[i][j]为字符串i变换到j的变换方法。

首先考虑S[i]!=T[j],这个好理解,因为不相等,所以考虑s中i这个元素和不考虑i这个元素结果是一样的,所以,dp[i][j] = dp[i-1][j],意思是如果当前字符不等,那么就只能抛弃当前这个字符。

接下来考虑S[i]==T[j](因为代码中下标从1开始,所以代码中是S[i-1]==T[j-1]),那么dp[i][j] = dp[i-1][j-1] + dp[i-1][j](这里可以理解为dp[i-1][j]+1,但是这里的1其实不是1,是之前出现过的结果,想想这里还是很好理解的。)。意思是:如果当前S[i]==T[j],那么当前这个字母即可以保留也可以抛弃,所以变换方法等于保留这个字母的变换方法加上不用这个字母的变换方法。

递归公式中用到的res[i][0] = 1(把任意一个字符串变换为一个空串只有一个方法)

class Solution {
public:
int numDistinct(string s, string t) {
//完全不能理解啊
int ls=s.size();
int lt=t.size();
if(lt==)
return ;
if(ls==)
return ;
vector<vector<int>> res(ls+,vector<int> (lt+,));
for(int i=;i<=ls;i++)
{
res[i][]=;
}
for(int i=;i<=ls;i++)
for(int j=;j<=lt;j++)
{
res[i][j]=res[i-][j];
if(s[i-]==t[j-])
res[i][j]=res[i-][j]+res[i-][j-];
}
return res[ls][lt]; }
};

 

Distinct Subsequences ——动态规划的更多相关文章

  1. LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静

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

  2. LeetCode之“动态规划”:Distinct Subsequences

    题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...

  3. Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

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

  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

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

  6. LeetCode(115) Distinct Subsequences

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

  7. [Leetcode][JAVA] Distinct Subsequences

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

  8. 30. Distinct Subsequences

    Distinct Subsequences OJ: https://oj.leetcode.com/problems/distinct-subsequences/ Given a string S a ...

  9. Distinct Subsequences——Leetcode

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

随机推荐

  1. [NOI2008] 道路设计

    link 思维题目,题目描述其实说的就是这是一个树,想到树形$dp$.若两个铁路不向交,则每个点的度都$\leq 2$.所以现在就可以搞dp了. 怎么去维护答案,容易想到设$dp(i,j,k)$为现在 ...

  2. LAMP架构的搭建 和wordpress

    [root@yu ~]#   yum install httpd php php-mysql mysql-server mysql -y 安装php [root@yu ~]# service http ...

  3. [solr]solr的安装

    solr是什么? 翻译: SolrTM is the popular, blazing fast open source enterprise search platform from the Apa ...

  4. web开发环境和要求配置

    对于eclipse,有很多版本,但要开发WEB程序,需要用到j2ee版本,如果是winform或android 用不带ee的版本就行,两者的明显区别是在看帮助->关于->Eclipse J ...

  5. 获取Web.Config中节点的值

    读取webconfig里面的appSetting和connectionString <appSettings> <add key="SiteURL" value= ...

  6. Azure Pipelines

    https://docs.microsoft.com/en-us/azure/devops/pipelines/?view=vsts

  7. MyBatis框架的使用及源码分析(十一) StatementHandler

    我们回忆一下<MyBatis框架的使用及源码分析(十) CacheExecutor,SimpleExecutor,BatchExecutor ,ReuseExecutor> , 这4个Ex ...

  8. Chrome profile manager

    由于Firefox有profile manager这么一说,所以自然联想到chrome应该也有. 默认chrome的profile manager被禁止了. 1. chrome://flags 2. ...

  9. Bzoj2832 / Bzoj3874 宅男小C

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 124  Solved: 26 Description 众所周知,小C是个宅男,所以他的每天的食物要靠外 ...

  10. hadoop+spark 集群的安装

    1.安装连接 https://www.cnblogs.com/zengxiaoliang/p/6478859.html