Solution: when see question about two strings , DP should be considered first.
We can abstract this question to calculate appear times for string T with length i in string S with length j, which can be represented by numbers[i][j], then through observation and thinking , we can know for numbers[i][j] it should at least equal the numbers[i][j-1] and if T.charAt(i)==S.charAt(j) , numbers[i][j] should also be add numbers[i-1][j-1]
 
  1. class Solution
  2. {
  3. public:
  4. int numDistinct(string S, string T) {
  5. int sLen = S.length(); int tLen = T.length();
  6. vector<vector<int>> dp(sLen+,vector<int>(tLen+));//dp[i][j]表示对应S前i个和T前j个字符的子问题。
  7. for (int i = ; i <= sLen; i++) dp[i][] = ;
  8. for (int i = ; i <= sLen; i++)
  9. {
  10. for (int j = ; j <= tLen; j++)
  11. {
  12. if (S[i - ] == T[j - ])
  13. {
  14. dp[i][j] = dp[i - ][j] + dp[i-][j-];
  15. }
  16. else
  17. {
  18. dp[i][j] = dp[i-][j];
  19. }
  20. }
  21. }
  22. return dp[sLen][tLen];
  23. }
  24.  
  25. };
  26. int main()
  27. {
  28. Solution s;
  29. string strS("b");
  30. string strT("a");
  31. cout << s.numDistinct(strS, strT) << endl;
  32. return ;
  33. }

http://rleetcode.blogspot.com/2014/01/distinct-subsequences-java.html

leetcode-distinct sequences的更多相关文章

  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. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

随机推荐

  1. maven——添加插件和添加依赖有什么区别?

    依赖:运行时开发时都需要用到的jar包,比如项目中需要一个Json的jar包,就要添加一个依赖,这个依赖在项目运行时也需要,因此在项目打包时需要把这些依赖也打包进项目里: 插件:在项目开的发时需要,但 ...

  2. try-catch里面加了return后,finally还会执行吗?

    请看下面的方法,在我们的catch里面,捕获到了异常之后,我们的catch模块里面的语句,还会接着执行,当我们执行到return之后,我们不会立即返回,而是会接着执行finally块里面的代码,只有执 ...

  3. 【OpenCV-Python】-图像阀值

    参考:Opencv官方教程 1.简单阀值 cv2.threshold , cv2.adaptiveThreshold当像素值高于阀值时,我们给这个像素赋予一个新值(可能是白色),否则我们给它赋予另外一 ...

  4. 汉诺塔问题java实现

    问题描述 三个柱子,起初有若干个按大小关系顺序安放的盘子,需要全部移动到另外一个柱子上.移动规则:在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘. 解题思路 使用递归算法进行处理,实在理不 ...

  5. 【ExtJS】一些基本概念的梳理

    学习ExtJS有一段时间了,一些相关知识点虽然每天都在用,不过如果猛的一问起来还是会一愣,趁现在好好梳理下吧.长期修改添加,弄清楚什么就加入什么. 1.Ext.onReady(): onReady() ...

  6. 入门系列之在Ubuntu上使用Netdata设置实时性能监控

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由小翼 发表于云+社区专栏 介绍 Netdata通过可扩展的Web仪表板提供准确的性能监控,可以显示Linux系统上的流程和服务.它监控 ...

  7. Java中final修饰参数的作用

    在方法参数前面加final关键字就是为了防止数据在方法体中被修改. 主要分两种情况:第一,用final修饰基本数据类型:第二,用final修饰引用类型. 第一种情况,修饰基本类型(非引用类型).这时参 ...

  8. Java反射获取当前项目下所有类,支持Servlet

    反射在很多时候要用,尤其自己编写框架时,那么如何获得当前项目下所有类呢!以下是本人封装的一个比较简洁的方法: [功能代码] //通过loader加载所有类 private List<Class& ...

  9. MDI-设置子窗体只能弹出一个--单例模式

    不足之处,欢迎指正! 什么是MDI..我表示不知道的呢. MDI(Multiple Document Interface)就是所谓的多文档界面,与此对应就有单文档界面 (SDI), 它是微软公司从Wi ...

  10. number_formate 货币金额或数量格式化

    $row['formated_goods_price']    = number_format($row['goods_price'], 2, '.', ''); number_format() 函数 ...