[抄题]:

给出字符串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. Python 示例 饮水记录

    因为每天都需要喝水  这是非常重要的 目录结构: ├─bin│ │ start.py│ ││ └─__pycache__│ start.cpython-36.pyc│├─core│ │ src.py│ ...

  2. 1076 Forwards on Weibo (30 分)

    1076 Forwards on Weibo (30 分) Weibo is known as the Chinese version of Twitter. One user on Weibo ma ...

  3. python3 钉钉群机器人 webhook

    import requests import json url='https://oapi.dingtalk.com/robot/send?access_token=替换成你自己的toten' pro ...

  4. iOS whose view is not in the window hierarchy!

    解决方法: viewController只Load完毕,没有Appear,此时应该将语句转移到ViewDidAppear方法中

  5. solr4.x之原子更新

    solr4.x发布以后,最值得人关注的一个功能,就是原子更新功能,传说的solr是否能真正的做到像数据库一样,支持单列更新呢? 在solr官方的介绍中,原子更新是filed级别的更新,不会涉及整个Do ...

  6. webpack(2)--Entry

    Entry entry是配置模块的入口,可以抽象成输入,webpack执行构建的第一步将从入口开始搜寻及递归解析出所有入口依赖的模块. 注意: entry是必填,若不填写则将导致webpack报错退出 ...

  7. 并发基础(四) java中线程的状态

    一.程的五种状态   线程的生命周期可以大致分为5种,但这种说法是比较旧的一种说法,有点过时了,或者更确切的来说,这是操作系统的说法,而不是java的说法.但对下面所说的六种状态的理解有所帮助,所以也 ...

  8. 了解ES6

    内容: 1.ES6介绍及基础 2.模块.类和继承 3.ES6高级特性 4.Generator和Iterator 5.异步编程 6.函数相关 内容参考:<ES6 标准入门> ES6标准阅读链 ...

  9. UVA548

    题意: 根据二叉树中序和后序建立二叉树,从根结点开始计算和到叶子结点,输出总和最小的叶子结点,如果有俩个和一样大,输出叶子结点最小的 AC:80ms #include<stdio.h> # ...

  10. 数据库中查询json 样式的值的sql语句

    参考:http://www.lnmp.cn/mysql-57-new-features-json.html 方式一: 可以查到json中的Key:value SELECT * FROM EDI.edi ...