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的更多相关文章

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

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

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

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

  3. [leetcode]Distinct Subsequences @ Python

    原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...

  4. Leetcode Distinct Subsequences

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

  5. [LeetCode] Distinct Subsequences 解题思路

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

  6. LeetCode: Distinct Subsequences [115]

    [称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...

  7. LeetCode: Distinct Subsequences 解题报告

    Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of  ...

  8. [LeetCode] Distinct Subsequences [29]

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

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

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

  10. 【LeetCode OJ】Distinct Subsequences

    Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...

随机推荐

  1. C++第二课:指针常用法[个人见解]

    在小编这里,没有任何学习知识的顺序,写到的东西对初学者肯定是有用处的,前提,你真的把C语言学完的那些初学者. 在讲明指针的知识前,或许有人一直说不会指针你学不会C++,或者说你所学C++的深度,全凭你 ...

  2. linux configure 应用

    linux下configure命令详细介绍 2018年01月11日 15:02:20 冷月霜 阅读数:705 标签: configure 更多 个人分类: 数据库技术   Linux环境下的软件安装, ...

  3. Docker安装及基本操作

    系统环境 CentOS Linux release 7.5.1804 (Core) 安装依赖包 更新系统软件 yum update 安装docker yum install docker 启动dock ...

  4. 整理SpringMVC

    Spring Web MVC核心架构图: 核心架构图流程如下: 1.首先用户发送请求------->DispatcherServlet(前端控制器),前端控制器收到请求后自己不进行处理,而是委托 ...

  5. 微信小程序开发---逻辑层(App Service)

    再说逻辑层之前,先说说微信小程序框架(MINA) 小程序开发框架的目标是通过尽可能简单.高效的方式让开发者可以在微信中开发具有原生APP体验的服务. 框架提供了自己的视图层描述语言WXML和WXSS, ...

  6. vue 路由跳转,传参

    一.直接跳转 //js1.this.$router.push('/ad_new') //html 2.<router-link to="/ad_check"> < ...

  7. 搭建 RTMP 服务器

    主要步骤 具体步骤 FAQ docker 搭建版 参考 主要步骤 下载 nginx 的 rtmp 模块 编译nginx,带 hls,rtmp 配置 nginx.conf,设置 rtmp 的推流文件路径 ...

  8. c# ef 排序字段动态,构建动态Lambda和扩展方法OrderBy

    1.动态构建排序 Lambda /// <summary> /// 获取排序Lambda(如果动态排序,类型不同会导致转换失败) /// </summary> /// < ...

  9. IPython绘图和可视化---matplotlib

    1. 启动 IPython 2. >> fig = plt.figure() >> ax1 = fig.add_subplot(346)          # 将画布分割成3行 ...

  10. 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑)

    背景介绍: 为了平衡社区成员的贡献和索取,一起帮引入了帮帮币.当用户积分(帮帮点)达到一定数额之后,就会“掉落”一定数量的“帮帮币”.为了增加趣味性,帮帮币“掉落”之后所有用户都可以“捡取”,谁先捡到 ...