115 Distinct Subsequences 不同子序列
给定一个字符串 S 和一个字符串 T,求 S 的不同的子序列中 T 出现的个数。
一个字符串的一个子序列是指:通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(譬如,"ACE"是"ABCDE" 的一个子序列,而 "AEC" 不是)
下面是一个例子:
S = "rabbbit", T = "rabbit"
返回 3.
详见:https://leetcode.com/problems/distinct-subsequences/description/
Java实现:
class Solution {
public int numDistinct(String s, String t) {
int m=s.length();
int n=t.length();
//dp[i][j]表示S串中从开始位置到第i位置与T串从开始位置到底j位置匹配的子序列的个数
int[][] dp = new int[m + 1][n + 1];
dp[0][0] = 1;//initial //如果S串为空,那么dp[0][j]都是0
for(int j = 1; j <= n; j++){//s is empty
dp[0][j] = 0;
} //如果T串为空,那么dp[i][j]都是1
for (int i = 1; i <= m; i++){//t is empty
dp[i][0] = 1;
} for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = dp[i - 1][j];
if (s.charAt(i - 1) == t.charAt(j - 1)) {
dp[i][j] += dp[i - 1][j - 1];
}
}
} return dp[m][n];
}
}
参考:https://www.cnblogs.com/springfor/p/3896152.html
115 Distinct Subsequences 不同子序列的更多相关文章
- [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]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, ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- leetcode 115 Distinct Subsequences ----- java
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 115. Distinct Subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 115. Distinct Subsequences (String; DP)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- 基于BASYS2的VHDL程序——数字钟(改进版)
扩展到时分秒.加了入调时电路,但不知道为什么有两个按键不好使.而且不知道以何种方式假如按键消抖电路,因为加入后会多个时钟控制一个信号,物理不可实现.调试电路待解决.还有,四个数目管中间的那两个圆点怎么 ...
- rc.local 开启自启动,检测是否成功
rc.local /etc/init.d/nginx start 查看运行状态 systemctl status rc-local ● rc-local.service - /etc/rc.local ...
- HTML(DOM)与JavaScript嵌套数组之间相互转换
html2ja:将html目标元素解析为JavaScript数组字面量,每项的值为tagName, className, id等CSS选择器组合: showJa:将html2ja生成的数组缩进格式化显 ...
- codeforces 465C.No to Palindromes! 解题报告
题目链接:http://codeforces.com/problemset/problem/464/A 题目意思:给出一个长度为 n 且是 tolerable 的字符串s,需要求出字典序最小的长度也为 ...
- yii中调取字段名称时label与labelEx的区别
$form = $this->beginWidget('CActiveForm',array('id' => 'userRegisterForm')); echo $form->la ...
- bzoj 3481 DZY Loves Math III——反演+rho分解质因数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3481 推推式子发现:令Q=gcd(P,Q),ans=Σ(d|Q) d*phi(P/d).把 ...
- ADB命令小结
)adb devices //查看启动的所有设备 )adb kill-server //重启设备 )adb start-server //启动设备 )adb -s emulator-(通过 adb d ...
- CPU、内存、硬盘分区的检测.py
cpu_mem_directories.py CPU.内存.硬盘分区的检测 #!/usr/bin/env python #coding:utf-8 import psutil import tim ...
- sybase SQL记录
在一个表中复制一行,主键是MLID ';
- Sherlock and the Encrypted Data
题意: 对于16进制数字num,假定 $p_0,p_1,...,p_m$ 在该数字中出现过,如果有 $x = 2^{p_0} + 2^{p_1} + ... + 2^{p_m}$ 且 $x \oplu ...