不同的子序列 · Distinct Subsequences
[抄题]:
给出字符串S和字符串T,计算S的不同的子序列中T出现的个数。
子序列字符串是原始字符串通过删除一些(或零个)产生的一个新的字符串,并且对剩下的字符的相对位置没有影响。(比如,“ACE”是“ABCDE”的子序列字符串,而“AEC”不是)。
Here is an example:
S = "rabbbit", T = "rabbit"
Return 3.
[思维问题]:
[一句话思路]:
由于要查找T。最后一位相同时可以同时删,不相同时只能删S,不能多删除T。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
怎么找出递推函数function:举实际的例子

[一刷]:
- f[0][0] =
- 扫描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的更多相关文章
- [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 ...
- [LeetCode 115] - 不同子序列(Distinct Subsequences)
问题 给出字符串S和T,计算S中为T的不同的子序列的个数. 一个字符串的子序列是一个由该原始字符串通过删除一些字母(也可以不删)但是不改变剩下字母的相对顺序产生的一个新字符串.如,ACE是ABCDE的 ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [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 ...
- [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 ...
- 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)
引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...
- Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划
Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...
- [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 ...
- [Leetcode] distinct subsequences 不同子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- linux下一个监测进程CPU和MEM使用率的shell脚本
#!/bin/bashPID=$1 cpu=`ps --no-heading --pid=$PID -o pcpu`mem=`ps --no-heading --pid=$PID -o pmem`ec ...
- Ubuntu12.10下Vsftpd的安装
安装Vsftpd sudo apt-get install vsftpd 配置 sudo vim /etc/vsftpd.conf 取消以下两行前面的注释 local_enable=YES write ...
- win10的坑之wifi找不到
安装了win10一周以来,win10的坑太多太多,微软搞什么pc/mobile二合一,真是脑残行为. 首先是usb设备无缘无故找不到,据说是和杀毒软件/防火墙有关,后来是关掉了windows defe ...
- Web 使用反射获得一个对象的所有get方法
问题描述: 由于想知道request中包含哪些getter方法,就想通过反射进行遍历,然后输出,结果异常,异常信息: 问题代码: try { outGetter(request); } catch ( ...
- 伯克利、OpenAI等提出基于模型的元策略优化强化学习
基于模型的强化学习方法数据效率高,前景可观.本文提出了一种基于模型的元策略强化学习方法,实践证明,该方法比以前基于模型的方法更能够应对模型缺陷,还能取得与无模型方法相近的性能. 引言 强化学习领域近期 ...
- VueCli
Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统,Vue CLI 致力于将 Vue 生态中的工具基础标准化.它确保了各种构建工具能够基于智能的默认配置即可平稳衔接,这样你可以专注在撰写 ...
- jenkins 构建一个maven项目
1.首先在 全局工具配置 里配置maven的路径信息 这里因为之前已经下载了maven并放在了E盘,因此只需要在 MAVEN_HOME 添加maven文件夹的路径 如若本地还没maven,勾选 “自动 ...
- Tomcat 支持的Java 版本和兼容性总结
https://tomcat.apache.org/whichversion.html 最新最全的Tomcat 支持的Java版本对照,即兼容性一览表: Servlet Spec JSP Spec ...
- 练手nginx反向代理和负载均衡apache实战
先说下原理性的 什么是反向代理 用户访问域名 域名的指向到nginx nginx把请求转发到apache apache处理后 返回给用户 整套的逻辑 对于用户来说 就是访问域名 然后返回 没 ...
- windows的类似shell 命令操作
一,类似linux的grep命令 参考网站:https://www.cnblogs.com/zxy1992/p/4372717.html windows执行cmd命令查找sql进程: C:\Users ...