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. angular 前台代码分层方法

    原代码: 现在将 findAll的get请求部分抽取成 服务,服务就是 $http.get 其实就是 ang内置的服务,其实就是可能会公用的方法,即可能被多个控制器调用的方法 比如这里认为 get请求 ...

  2. Python图像处理库(PIL)

    官方:(详细)http://pillow.readthedocs.io/en/3.1.x/reference/ImageDraw.html http://pillow.readthedocs.io/e ...

  3. HDU 4348 主席树区间更新

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  4. Codeforces Round #307 (Div. 2) D 矩阵快速幂+快速幂

    D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...

  5. jsp链接sql数据库

    Connection 参数//这个参数用来执行链接数据库的操作 String 参数2="com.microsoft.sqlserver.jdbc.SQLServerDriver"; ...

  6. 阿里C++研发实习二面和三面面经

    下午连着面了阿里爸爸的二面和三面,非常不明白别人的三面都是hr了,为什么我还是在技术面,难道面了个假阿里.不管怎么样,来篇面经攒攒人品. 二面 第一次遇到这么严肃的面试官,居然可以全程无表情的,面了这 ...

  7. [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

  8. Golang向Templates 插入对象的值

    Go对象可以插入到template中,然后把对象的值表现在template中,你可以一层层的分解这个对象,去找他的子字段,当前对象用'.'来表示,所以当当前对象是一个string的时候,你可以用{{. ...

  9. Item 8 覆盖equals时请遵守通用约定

    在覆盖equals方法的时候,你必须要遵守它的通用约定,不遵守,写出来的方法,会出现逻辑错误.下面是约定的内容:   equals方法实现了等价关系:   自反性.对于任何非null的引用值,x.eq ...

  10. 在使用ubuntu16.04+python3.5 下使用pip3出现pip3 error - '_NamespacePath' object has no attribute 'sort'

    使用pip3安装tensorflow以及gensim等时,出现如下错误: Traceback (most recent call last): File "/usr/local/bin/pi ...