[抄题]:

给出字符串S和字符串T,计算S的不同的子序列中T出现的个数。

子序列字符串是原始字符串通过删除一些(或零个)产生的一个新的字符串,并且对剩下的字符的相对位置没有影响。(比如,“ACE”是“ABCDE”的子序列字符串,而“AEC”不是)。

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

Return 3.

[思维问题]:

[一句话思路]:

由于要查找T。最后一位相同时可以同时删,不相同时只能删S,不能多删除T。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

怎么找出递推函数function:举实际的例子

[一刷]:

  1. f[0][0] =
  2. 扫描s的每个字母前先初始化,在过程中累加

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

草稿上把+=写清楚

"aab"
"ab"

[总结]:

怎么找出递推函数function:举实际的例子。

求个数时,一般情况是等于,只有重复情况下 个数增多才是相加。

[复杂度]:Time complexity: O(n^2) Space complexity: O(n^2)

DP先想暴力解法:Cn x 不行

[英文数据结构或算法,为什么不用别的数据结构或算法]:

双序列DP

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

5. Longest Palindromic Substring 回文串最值 用dp

[代码风格] :

public class Solution {
/*
* @param : A string
* @param : A string
* @return: Count the number of distinct subsequences
*/
public int numDistinct(String s, String t) {
//state
int m = s.length();
int n = t.length();
int[][] f = new int[m + 1][n + 1];
//initialization
//m == 0
for (int i = 0; i <= n; i++) {
f[0][i] = 0;
}
// n == 0
for (int j = 0; j <= m; j++) {
f[j][0] = 1;
}
//function
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
f[i][j] = f[i - 1][j];
if (s.charAt(i - 1) == t.charAt(j - 1)) {
f[i][j] += f[i - 1][j - 1];
}
}
}
//answer
return f[m][n];
}
};

不同的子序列 · Distinct Subsequences的更多相关文章

  1. [Swift]LeetCode115. 不同的子序列 | Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  2. [LeetCode 115] - 不同子序列(Distinct Subsequences)

    问题 给出字符串S和T,计算S中为T的不同的子序列的个数. 一个字符串的子序列是一个由该原始字符串通过删除一些字母(也可以不删)但是不改变剩下字母的相对顺序产生的一个新字符串.如,ACE是ABCDE的 ...

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

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

  4. [Swift]LeetCode940. 不同的子序列 II | Distinct Subsequences II

    Given a string S, count the number of distinct, non-empty subsequences of S . Since the result may b ...

  5. [leetcode]115. Distinct Subsequences 计算不同子序列个数

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

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

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

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

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

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

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

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

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

随机推荐

  1. Windows下python 安装Mysqldb模块

    CMD执行 pip install mysql-python 报错如下: 1.如果报类似 Microsoft Visual C++ 9.0 is required < Unable to fin ...

  2. 利用.pbk来实现ADSL开机自动拨号

    当你新建拨号连接或者VPN连接之后在你的电脑里会创建一个.pbk的文件 这个.pbk的文件可以说是一个集合,将你电脑的所有连接都保存在一起. 同时你还可以将此连接复制起来传给其他人. 系统默认的.pb ...

  3. [UE4]计算机中的数据表示

    一.位:数据的最小单位,一个位仅有两种状态 一个电路,通或断:磁盘上的小磁铁,南极或北极: 使用Bit表示位,百兆带宽,一秒钟可以传输一百兆个bit 二.字节:8个位组成一个字节,一个字节有256种状 ...

  4. [UE4]角色增加挂点、增加枪

    人物骨骼增加Socket(骨骼) 增加手持武器预览: 角色蓝图增加组件“Skeletal Mesh”(好像这叫骨骼模型吧),并拖放至人物“Mesh”下面作为子组件. 选中刚建好的“SkeletalMe ...

  5. Lua语言中的__index,__newindex,rawget和rawset

    转自:http://blog.csdn.net/wangbin_jxust/article/details/12108189 在谈及Lua中的__index,__newindex,rawget和raw ...

  6. 【Python编程:从入门到实践】chapter5 if语句

    chapter5 if语句5.1 一个简单示例cars = ['audio','bmw','subaru','toyota'] for car in cars:if car == 'bmw':prin ...

  7. Java并发编程学习路线(转)

    以前特地学过并发编程,但是没怎么学进去,不太喜欢.最近发现,作为一个资深工程师,却没有完整深入系统的学习过,而反是现在的BAT大并发是必须的,感觉甚是惭愧. 故找了一片学习文章,如下,准备集中一段时间 ...

  8. python学习笔记一和PHP的一些对比

    python和php一样是 解释性语言 php和php一样 定义变量不需要指定类型,C语言中的print函数 在python中也是适用的 python编码 适用缩进  4个空格,通常存储为UTF-8模 ...

  9. OpenACC 计算圆周率(简单版)

    ▶ 书上的计算圆周率的简单程序,主要是使用了自定义函数 #include <stdio.h> #include <stdlib.h> #include <math.h&g ...

  10. java中正则表达式,编译报错:Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

    转自:https://www.cnblogs.com/EasonJim/p/6561666.html 若出现:Invalid escape sequence (valid ones are  \b   ...