leetcode — distinct-subsequences
import java.util.Arrays;
/**
*
* Source : https://oj.leetcode.com/problems/distinct-subsequences/
*
*
* 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.
*/
public class DistinctSubsequences {
/**
*
* 求解的个数,使用动态规划
*
* 状态:
* i,j表示T中长度为i的prefix:T[0:i-1],S中长度为j的prefix:S[0-j-1],S[j]第j个字符,T[i]第i个字符
* DP[i][j]表示:S[0:j]中包含T[0:i]唯一子串的个数,当j<i的时候DP[i][j] = 0
*
* 递推公式:
* 当S[j] != T[i]的时候
* DP[i+1][j+1] = DP[i+1][j],含义是当前字符不相等的时候,S[j+1]包含T[i+1]的个数就是S[j]包含T[i+1]的个数
*
* 当S[j] == T[i]的时候
* DP[i+1][j+1] = DP[i+1][j] + DP[i][j],含义是当前字符相等的时候,S[j+1]包含T[i+1]的个数就是S[j]包含T[i+1]的个数加上S[j]包含T[i]的个数
*
* 计算方向和起始状态:
* DP[i][j]
* DP[i+1][j],DP[i+1[j+1]
* 从上到下,从左到右
*
* 第0行:1
* 第0列:0
*
*
* @param S
* @param T
* @return
*/
public int distinctSequences (String S, String T) {
int[][] dp = new int[T.length()+1][S.length()+1];
for (int i = 0; i <= T.length(); i++) {
dp[i][0] = 0;
}
for (int i = 0; i <= S.length(); i++) {
dp[0][i] = 1;
}
for (int i = 1; i <= T.length(); i++) {
for (int j = i; j <= S.length(); j++) {
if (S.charAt(j-1) == T.charAt(i-1)) {
dp[i][j] = dp[i][j-1] + dp[i-1][j-1];
} else {
dp[i][j] = dp[i][j-1];
}
}
}
return dp[T.length()][S.length()];
}
/**
* 优化DP占用空间,因为递推的时候只需要dp[i][j-1],dp[i-1][j-1]
* 也就是当前矩阵左上角的值和左面的值,使用滚动数组优化空间
*
* @param S
* @param T
* @return
*/
public int distinctSequences1 (String S, String T) {
int[] dp = new int[S.length()+1];
Arrays.fill(dp, 1);
for (int i = 1; i <= T.length(); i++) {
int upLeft = dp[0];
dp[0] = 0;
for (int j = 1; j <= S.length(); j++) {
// 相当于记下dp[i-1][j-1]
int temp = dp[j];
// 相当于dp[i][j-1]
dp[j] = dp[j-1];
if (S.charAt(j-1) == T.charAt(i-1)) {
dp[j] += upLeft;
}
upLeft = temp;
}
}
return dp[S.length()];
}
public static void main(String[] args) {
DistinctSubsequences subsequences = new DistinctSubsequences();
System.out.println(subsequences.distinctSequences("rabbbit", "rabbit") + "------3");
System.out.println(subsequences.distinctSequences1("rabbbit", "rabbit") + "------3");
}
}
leetcode — distinct-subsequences的更多相关文章
- 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)
引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [leetcode]Distinct Subsequences @ Python
原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [LeetCode] Distinct Subsequences 解题思路
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode: Distinct Subsequences [115]
[称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...
- LeetCode: Distinct Subsequences 解题报告
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- [LeetCode] Distinct Subsequences [29]
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode] distinct subsequences 不同子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
随机推荐
- BZOJ.4145.[AMPPZ2014]The Prices(状压DP)
BZOJ 比较裸的状压DP. 刚开始写麻烦惹... \(f[i][s]\)表示考虑了前\(i\)家商店,所买物品状态为\(s\)的最小花费. 可以写求一遍一定去\(i\)商店的\(f[i]\)(\(f ...
- Python的使用方法
1 安装turtle Python2安装命令: pip install turtule Python3安装命令: pip3 install turtle 因为turtle库主要是在Python2中使用 ...
- npm、cnpm、yarn 安装删除异同
背景 一直觉得npm.cnpm.yarn的安装删除基本一样用哪个都行,不过俗话说的好,实践出真知,这里记录一下今天简单测试得到的结果总结. 可能会有错误,希望大家评论指正,十分感谢. 测试电脑系统:M ...
- java DTO对象与PO对象的相互转换
2018-09-27 10:27:50 前言: 在实际开发中往往需要DTO对象与PO对象的相互转换: 先说说什么是DTO对象吧,个人觉得DTO就是PO的扩展而已,PO专门指向数据库,DTO作扩展(字段 ...
- 请问浏览器访问www.baidu.com经历了怎样的过程?
1.查找浏览器缓存 首先会查找浏览器缓存,浏览器会保存一段时间你之前访问过的网址的DNS信息,不同的浏览器保存的时长不等. 2.查找系统缓存 如果上面的步骤没有找到对应的DNS信息,这个时候浏览器会尝 ...
- python-简单邮件报警
在scrapy爬虫项目中经常遇到 爬取数据时报错无法及时处理 导致数据爬取不完整 只能先查看log才能发现报错 首先写一个简单的邮件发送模块 """ @file: ema ...
- ChartControl设置坐标轴范围
需求:有一条坐标轴是用来表示百分比的,所以刻度范围只能是从0 - 100: 解决方法:运行chatcontrol设计器,选中需要设置的轴,如下图: 设置Whole range 的最大最小值: 默认情况 ...
- SPARK-AM-TrackURL-UI-500
HTTP ERROR 500 Problem accessing /proxy/application_1538120222810_0072/. Reason: Connection refused ...
- HBuilder git使用-建立仓库,邀请用户
1.git环境配置好后,在Github上注册好帐号 2. 创建一个Respository(代码仓库) 3.邀请其他小组用户(必须的,要不别人提交不了修改) 4.把邀请链接要COPY给其他用户 5. 其 ...
- Daily Pathtracer!安利下不错的Pathtracer学习资料
0x00 前言 最近看到了我司大网红aras-p(Aras Pranckevičius)的博客开了一个很有趣的新系列<Daily Pathtracer~>,来实现一个简单的ToyPathT ...