题目

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"

Return3.

思路

1. 初始化一个矩阵number[i][j]用来记录字符串T的前j个字符出现在字符串S的前i个字符的次数,当j=0时,令number[i][j]=1;

2. 当S的第i个字符与T的第j个字符不同时,则说明S的第i个字符对number[i][j]没有影响,即number[i][j]=number[i-1][j];

3. 当S的第i个字符与T的第j个字符不同时,则说明S的第i个字符对number[i][j]有影响,number[i][j]除了要算上原来的number[i-1][j],还要算上新的可能性,即number[i-1][j-1].

例子

  0 r a b b i t
0 1 0 0 0 0 0 0
r 1 1 0 0 0 0 0
a 1 1 1 0 0 0 0
b 1 1 1 1 0 0 0
b 1 1 1 2 1 0 0
b 1 1 1 3 3 0 0
i 1 1 1 3 3 3 0
t 1 1 1 3 3 3 3

代码

     public static int result(String str1, String str2){
int len1 = str1.length(), len2 = str2.length();
int[][] res = new int[len1+1][len2+1];
for(int i=0;i<len1+1;i++){
res[i][0] = 1;
}
for(int i=1;i<len1+1;i++){
for(int j=1;j<len2+1;j++){
if(str1.charAt(i-1)!=str2.charAt(j-1))
res[i][j] = res[i-1][j];
else
res[i][j] = res[i-1][j]+res[i-1][j-1];
} }
return res[len1][len2];
}

动态规划—distinct-subsequences的更多相关文章

  1. 动态规划——Distinct Subsequences

    题目大意:给定字符串S和T,现在从S中任选字符组成T,要求输出方案个数. Example 1:Input: S = "rabbbit", T = "rabbit" ...

  2. 动态规划-Distinct Subsequences

    2020-01-03 13:29:04 问题描述: 问题求解: 经典的动态规划题目,一般来说dp题目是递推关系公式难想,但是实际代码量还是比较少的. 有尝试过dfs来做,但是由于时间复杂度是指数级别的 ...

  3. LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静

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

  4. 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 s ...

  6. Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

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

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

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

  8. Leetcode Distinct Subsequences

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

  9. LeetCode(115) Distinct Subsequences

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

  10. [Leetcode][JAVA] Distinct Subsequences

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

随机推荐

  1. UNR#3 Day1——[ 堆+ST表+复杂度分析 ][ 结论 ][ 线段树合并 ]

    地址:http://uoj.ac/contest/45 第一题是鸽子固定器. 只会10分.按 s 从小到大排序,然后 dp[ i ][ j ][ k ] 表示前 i 个元素.已经选了 j 个.最小值所 ...

  2. SpringCloud 教程 (四) docker部署spring cloud项目

    一.docker简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机). ...

  3. CG-CTF | 综合题2

    查源码发现一个文件读取:http://cms.nuptzj.cn/about.php?file=sm.txt,用它把能找到的php都读取下来 <?php if (!isset($_COOKIE[ ...

  4. 170830-关于JdbcTemplate的练习题以及其中的问题

    实验1:测试数据源 在spring文件中配置: 测试数据源: 结果: 实验2:将emp_id=5的记录的salary字段更新为1300.00[更新操作] update函数中,第一个是sql语句,后面的 ...

  5. Elasticsearch结构化搜索与查询

    Elasticsearch 的功能之一就是搜索,搜索主要分为两种类型,结构化搜索和全文搜索.结构化搜索是指有关查询那些具有内在结构数据的过程.比如日期.时间和数字都是结构化的:它们有精确的格式,我们可 ...

  6. 关于openGL、GPUImage、ios直播相关不错的博客

    http://www.jianshu.com/users/815d10a4bdce/latest_articles

  7. navigation ObtacleCostFunction源码分析

    ObtacleCostFunction 定义了一个ObstacleCostFunction类,继承自Trajectory类,Trajectory类有8个类参 总共有8个类参 double xv_,yv ...

  8. skip a transaction in goldengate

    skip a transaction in goldengate [oracle@db ]$ ggsci Oracle GoldenGate Command Interpreter for Oracl ...

  9. python安装使用(windows)

    安装 参考:http://scrapy-chs.readthedocs.io/zh_CN/0.24/intro/install.html#scrapy 用到的文件:https://share.weiy ...

  10. Delphi XE2 之 FireMonkey 入门(18) - TLang(多语言切换的实现)

    一个小小的 TLang 类, 实现多语言切换, 挺好的. 它的工作思路是: 1.首先通过 AddLang('语言代码') 添加语言类别, 如: AddLang('en').AddLang('cn'). ...