题目

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

代码

  1. public static int result(String str1, String str2){
  2. int len1 = str1.length(), len2 = str2.length();
  3. int[][] res = new int[len1+1][len2+1];
  4. for(int i=0;i<len1+1;i++){
  5. res[i][0] = 1;
  6. }
  7. for(int i=1;i<len1+1;i++){
  8. for(int j=1;j<len2+1;j++){
  9. if(str1.charAt(i-1)!=str2.charAt(j-1))
  10. res[i][j] = res[i-1][j];
  11. else
  12. res[i][j] = res[i-1][j]+res[i-1][j-1];
  13. }
  14.  
  15. }
  16. return res[len1][len2];
  17. }

动态规划—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. SpringCloud 教程 (七)服务注册(consul)

    一.consul 简介 consul 具有以下性质: 服务发现:consul通过http 方式注册服务,并且服务与服务之间相互感应. 服务健康监测 key/value 存储 多数据中心 consul可 ...

  2. E. You Are Given Some Strings...

    E. You Are Given Some Strings... AC自动机 求一个串$t$中包含子串$s_{i}+s_{j}$的个数. 可以正反跑两遍AC自动机 正着跑,表示$s_{i}$结束,反正 ...

  3. 【转】vux (scroller)上拉刷新、下拉加载更多

    1)比较关键的地方是要在 scroller 组件上里加一个 ref 属性 <scroller :lockX="true" height="-170" :p ...

  4. uniapp 之navigateTo:fail page 跳转路径不对

    开发uniapp,点击列表跳转详情报错 [system] navigateTo:fail page `/pages/tabBar/index/detail/detail?title=uni-app行业 ...

  5. 从Java 调用JavaScript

    篇幅过长 下载资源链接https://www.slidestalk.com/s/java_javascript_5hl09w

  6. 史上最详细的XGBoost实战

    史上最详细的XGBoost实战 0. 环境介绍 Python 版 本: 3.6.2 操作系统 : Windows 集成开发环境: PyCharm 1. 安装Python环境 安装Python 首先,我 ...

  7. Azure Monitor Kibana configuration always seems to send over SSL

    https://github.com/elastic/logstash/issues/10125 https://blogs.cisco.com/security/step-by-step-setup ...

  8. Ubuntu 系统搭建LNMP环境

    当前Linux版本:Ubuntu16.04 一.安装Nginx 在终端中输入命令 " sudo apt-get install nginx ",在确认安装完成后,在浏览器中访问 l ...

  9. p4841 城市规划

    分析 https://www.luogu.org/blog/DRA/solution-p4841 代码(似乎附赠了一个全家桶呢) #pragma GCC optimize(2) #pragma GCC ...

  10. 监听整个页面上的DOM树变化

    在线预览 方法 使用<Web API 接口>的<MutationObserver> MutationObserver 网上查到的很多都是使用Mutation events的,但 ...