Distinct Subsequences ——动态规划

时间:2022-10-20 09:09:32

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之&OpenCurlyDoubleQuote;动态规划”: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. &lbrack;LeetCode&rsqb; 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. &lbrack;Leetcode&rsqb;&lbrack;JAVA&rsqb; Distinct Subsequences

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

  8. 30&period; 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. Create New Commands in Tcl

    Create New Commands in Tcl eryar@163.com 摘要Abstract:Tcl/Tk脚本可以很容易实现用户自定义的命令,方便的创建图形化的用户界面GUI,所以Tcl和T ...

  2. Java基础之写文件——将素数写入文件中(PrimesToFile)

    控制台程序,计算素数.创建文件路径.写文件. import static java.lang.Math.ceil; import static java.lang.Math.sqrt; import ...

  3. webpack学习最基本的使用方式(一)

    网页中引入的静态资源多了以后会有什么问题.? 1.网页加载速度慢,因为我们要发起很多的二次请求 2.要处理错综复杂的依赖关系 如何解决上面的问题 1.合并,压缩图片,使用精灵图 2.可以使用之前学过的 ...

  4. Linux关键字查询

    grep -R "查询关键字" /目录/*

  5. ef5 数据库操作

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. 如何根据Ip获取地址信息--Java----待整理完善!!!

    如何根据Ip获取地址信息--Java----待整理完善!!! QQWry.dat数据写入方法: http://www.cnblogs.com/xumingxiang/archive/2013/02/1 ...

  7. Python获取指定路径下所有文件的绝对路径

    需求 给出制定目录(路径),获取该目录下所有文件的绝对路径: 实现 方式一: import os def get_file_path_by_name(file_dir): ''' 获取指定路径下所有文 ...

  8. greasemonkey

    Greasemonkey Hacks/Getting Started < Greasemonkey Hacks Greasemonkey Hacks Foreword Credits Prefa ...

  9. python014 Python3 迭代器与生成器

    Python3 迭代器与生成器迭代器迭代是Python最强大的功能之一,是访问集合元素的一种方式..迭代器是一个可以记住遍历的位置的对象.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结 ...

  10. windows如何统计端口的连接数

    习惯了linux的系统管理员,对linux的命令行工具总是印象极深,几乎所有的管理都可以在命令行下完成.命令行工具是linux系统管理的主流. 而使用windows是,因为图形化的界面,大家习惯了图形 ...