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]
 
 class Solution
{
public:
int numDistinct(string S, string T) {
int sLen = S.length(); int tLen = T.length();
vector<vector<int>> dp(sLen+,vector<int>(tLen+));//dp[i][j]表示对应S前i个和T前j个字符的子问题。
for (int i = ; i <= sLen; i++) dp[i][] = ;
for (int i = ; i <= sLen; i++)
{
for (int j = ; j <= tLen; j++)
{
if (S[i - ] == T[j - ])
{
dp[i][j] = dp[i - ][j] + dp[i-][j-];
}
else
{
dp[i][j] = dp[i-][j];
}
}
}
return dp[sLen][tLen];
} };
int main()
{
Solution s;
string strS("b");
string strT("a");
cout << s.numDistinct(strS, strT) << endl;
return ;
}

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. spring、springmvc和mybatis整合(xml方式)

    今天搭建一个基于xml的ssm整合demo.话不多说,直接上代码. 我的开发环境如下: web服务器:tomcat8 开发工具:STS JDK版本:1.8 项目构建工具:maven 1.pom.xml ...

  2. EasyNetQ自定义异常消息处理

    20140310补充: rabbitmq有requeue属性,可以选择消息是否返回队列,另,本文的解决方式非常之山寨,只能应用于发送和接收方式. 这几天在折腾消息队列,在.Net环境下有基于Rabbi ...

  3. Android 开发 命名规范(基础回顾)

    标识符命名法标识符命名法最要有四种: 1 驼峰(Camel)命名法:又称小驼峰命名法,除首单词外,其余所有单词的第一个字母大写. 2 帕斯卡(pascal)命名法:又称大驼峰命名法,所有单词的第一个字 ...

  4. 2.2、js基础---预解析和严格模式

    一.语言特性         1.预解析:js会把变量的声明(仅仅是声明)提到顶部,但是不会突破作用域.                 alert(a);var a= 12; //结果,undefi ...

  5. RabbitMQ入门-理论

    目录 RabbitMQ简介 RabbitMQ原理简介 RabbitMQ安装 .NET Core 使用 RabbitMQ Hello World 工作队列 扇型交换机 直连交换机 主题交换机 远程过程调 ...

  6. BNU44583——Star Trek: First Contact——————【01背包】

    H. Star Trek: First Contact Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld  ...

  7. canvas绘制经典星空连线效果

    来自:https://segmentfault.com/a/1190000009675230 下面开始coding:先写个canvas标签 <canvas height="620&qu ...

  8. C#之RabbitMQ系列(一)

    RabbitMQ–环境搭建 MQ MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接 ...

  9. Java 基础(5)——数据转换 & 特殊的引用类型

    数据转换 变量在第(3)篇中有讲到过八种数据类型,分别是能够用来表示整型的 byte.short.int.long 和表示浮点型的 float.double 以及字符型 char.布尔型 boolea ...

  10. shell输入与输出功能

    一.shell输入功能 1. 2. 二.shell输出功能 1.字符界面前景颜色 2.字符界面背景颜色 3.其他输出命令 ①cat 输出文本,将文本的格式也输出 ②tee 既输出,也保存到文件里 ③m ...