题目

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. 洛谷P1199 三国游戏——题解

    题目传送 显然,在这样的数据范围下搜索是没希望的了.好好分析一下,发现小涵时不可能拿到与一个武将最默契的另一个武将了.所以考虑一下默契值次大的一对武将. 显然,对每一个武将来说,小涵是可以拿到默契值次 ...

  2. SpringCloud 教程 (一) 消息总线(Spring Cloud Bus)

    Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务 ...

  3. AC自动机再加强版

    AC自动机 拓扑排序优化,注意拓扑排序前要把所有入度为零的点都加进去 #include<bits/stdc++.h> using namespace std; #define maxn 1 ...

  4. es之java索引操作

    1.7.1: 创建索引 /** * 创建索引 * */ @Test public void createIndex(){ // 创建索引 CreateIndexResponse blog2 = cli ...

  5. oracle数据泵导入导出

    1.首先建立DUMP_DIR sqlplus / as sysdba select * from dba_directories 如果没有DUMP_DIR就执行下面的语句 CREATE OR REPL ...

  6. jQuery UI Widget Factory

    https://learn.jquery.com/jquery-ui/widget-factory/ The jQuery UI Widget Factory is an extensible bas ...

  7. CentOS yum 安装历史版本 java

    1.以1.6为例,找到对应版本 $ yum --showduplicate list java* |grep 1.6 java--openjdk.x86_64 :1.6.0.41-1.13.13.1. ...

  8. 熟练使用jquery ui / easyui的各种组件设计网页界面

    熟练使用jquery ui / easyui的各种组件设计网页界面

  9. 架构-层-BLL:BLL

    ylbtech-架构-层-BLL:BLL 业务逻辑层(Business Logic Layer)无疑是系统架构中体现核心价值的部分.它的关注点主要集中在业务规则的制定.业务流程的实现等与业务需求有关的 ...

  10. linux(centos6.5)常用命令

    前言:由于项目项目使用的是linux服务器,因此会使用到较多linux命令,本文对centos下常用命令进行记录 1.vi的三种模式 2.解压缩相关 3.用户相关 4.文件相关 5.各种查看命令 1. ...