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.

其实这道题本来不是很难,因为一看就是动态规划。结果笨娃(哎,真是笨娃啊)搞这个递推式搞了很久。所以记录一下。

Instinctly,(真心发现既然是dp,就往这方面想就是)假设S取前面i个字符(最后一个index是i - 1), T取前面j个字符(最后一个index是j - 1),那么在前i个字符中,有序列的个数num[i][j]的公式应该怎么写呢?

首先,如果S[i - 1] 不等于T[j - 1],那么num[i][j] = num[i - 1][j]。不难理解啊,就是S往前缩一个呗,反正也匹配不了T[j - 1]是不是?

如果S[i - 1]等于T[j - 1], 那么,num[i][j]应该是: num[i - 1][j - 1] + num[i - 1][j]。

为啥呢?

num[i - 1][j - 1]: S中还没到i的同志们在翘首盼望着i ,同样T中的乡亲们也在等待j 。符合条件的S[i - 1]一到达,他们就自然加入到num[i][j]的队伍中了,如下图所示。

num[i - 1][j]: S还没到i,但其中一些同志们已经满足T[0: j-1]。符合条件的S[i - 1]到达,他们需要也加入到num[i][j]的队伍中,如下图所示。

(是不是觉得楼主疯了)

聪明的同学肯定要问了,但是初始状态大家都是0,没见到+1啊。这里有个特殊的初始化,就是num[i][0] = 1。这好像在说,空串始终匹配整个串S

代码如下:

public int numDistinct(String S, String T) {
if (T.length() > S.length()) {
return 0;
}
int[][] num = new int[S.length() + 1][T.length() + 1];
for (int i = 0; i <= S.length(); i++) {
num[i][0] = 1;
}
for (int i = 1; i <= S.length(); i++) {
for (int j = 1; j <= Math.min(i, T.length()); j++) {
if (S.charAt(i - 1) == T.charAt(j - 1)) {
num[i][j] = num[i - 1][j - 1] + num[i - 1][j];
} else {
num[i][j] = num[i - 1][j];
}
}
} return num[S.length()][T.length()];
}

LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静的更多相关文章

  1. 【leetcode刷题笔记】Distinct Subsequences

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

  2. 【一天一道LeetCode】#115. Distinct Subsequences

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【LeetCode】115. Distinct Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  4. 【LeetCode】940. Distinct Subsequences II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  5. 【LeetCode OJ】Distinct Subsequences

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

  6. Distinct Subsequences ——动态规划

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

  7. leetcode -day 15 Distinct Subsequences

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

  8. 【LeetCode】114. Distinct Subsequences

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

  9. 【leetcode】940. Distinct Subsequences II

    题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...

随机推荐

  1. java网络---流

    网络操作很大一部分功能就是输入和输出数据. 简单归纳就是上传和下载文件.文件也是数据的一种载体. java对数据的操作归并为流. 所以对于数据流的操作定义2个基本类. java.io.OutputSt ...

  2. 敏捷软件开发:原则、模式与实践——第8章 SRP:单一职责原则

    第8章 SRP:单一职责原则 一个类应该只有一个发生变化的原因. 8.1 定义职责 在SRP中我们把职责定义为变化的原因.如果你想到多于一个的动机去改变一个类,那么这个类就具有多于一个的职责.同时,我 ...

  3. Chrome 控制台 console

    前言 Chrome浏览器我想是每一个前端er必用工具之一吧,一部分原因是它速度快,体积不大,支持的新特性也比其它浏览器多,还有一部分我想就是因为它的控制台功能强大了吧,说它是神器一点也不过分,很方便. ...

  4. 在Web api2 中传递复杂参数的一点心得

    这两天在做的一个项目基于webapi2,期间遇到了复杂参数传递的问题.其中刚好看到园友的这篇文章,但是我测试收结果是失败的,还不知道是什么原因.最终经过思考后,找到了一种方法,和大家分享下. 在前端我 ...

  5. Java中的显示锁 ReentrantLock 和 ReentrantReadWriteLock

    在Java1.5中引入了两种显示锁,分别是可重入锁ReentrantLock和可重入读写锁ReentrantReadWriteLock.它们分别实现接口Lock和ReadWriteLock.(注意:s ...

  6. 【mysql】索引的优化

    写在前面的话 查询容易,优化不易,且写且珍惜 mysql结构 从MySQL逻辑架构来看,MySQL有三层架构,第一层连接,第二层查询解析.分析.优化.视图.缓存,第三层,存储引擎 MySQL有哪些索引 ...

  7. 公用表表达式(CTE)引发的改变执行顺序同WHERE条件顺序引发的bug

    以下模拟一下CTE出错 /*测试环境 Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) Apr 2 2010 15:48:46 Copyr ...

  8. python collections,函数等笔记

    笔记 # -*- coding:utf-8 -*- #需求:一个列表里大于66的元素添加字典的第二个key k2里,小于66的添加第一个 key k1里 li = [1,2,3,4,5,6,67,7, ...

  9. hdu 2199 Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  10. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...