【称号】

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"

Return 3.

【题意】

给定字符串S和T,通过删除S中的若干个字符能够得到T,则T称为S的子序列。问有多少种删法能够得到T

【思路】

DP问题。

    定义A[i][j] (i>j)表示S[1...i]转化到T[1...j]的方式数(操作类型仅仅有一种。那就是从S中删除若干字符)。

    转换方程例如以下:

        假设S[i]==T[j], A[i][j]=A[i-1][j-1]+A[i-1][j];

        假设S[i]!=T[j], A[i][j]=A[i-1][j];

        

    初始化矩阵

        起始点A[0][0]=1;

        第一行A[0][i]=0;

        第一列A[i][j]=1;

【代码】

class Solution {
public:
int numDistinct(string S, string T) {
if(S.length()==0 || S.length()==0)return 0;
if(S.length()<T.length())return 0; //假设S==T,返回1, 觉得有1种转换方式 vector<vector<int> >matrix(S.length()+1, vector<int>(T.length()+1, 0));
//初始化matrix[0][0]
matrix[0][0]=1;
//初始化对角线
for(int j=1; j<=T.length(); j++)
matrix[0][j]=0;
//初始化第一列
for(int i=1; i<=S.length(); i++)
matrix[i][0]=1; //考虑其它i>j的情况
for(int i=1; i<=S.length(); i++){
for(int j=1; j<=T.length(); j++){
if(S[i-1]==T[j-1])matrix[i][j]=matrix[i-1][j-1]+matrix[i-1][j];
else matrix[i][j]=matrix[i-1][j];
}
}
return matrix[S.length()][T.length()];
}
};

版权声明:本文博客原创文章。博客,未经同意,不得转载。

LeetCode: Distinct Subsequences [115]的更多相关文章

  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 解题报告

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

  7. [LeetCode] Distinct Subsequences [29]

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

  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 S which equals T. A su ...

随机推荐

  1. OCA读书笔记(16) - 执行数据库恢复

    16. Performing Database Recovery 确定执行恢复的必要性访问不同接口(EM以及命令行)描述和使用可用选项,如RMAN和Data Recovery Advisor执行恢复- ...

  2. Pyocr 0.2 发布,Python 的 OCR 库 - 开源中国社区

    Pyocr 0.2 发布,Python 的 OCR 库 - 开源中国社区 Pyocr 0.2 发布,Python 的 OCR 库

  3. Java中的工具类和新特性

    1:Collections集合框架工具类: /* 集合框架的工具类. Collections:集合框架的工具类.里面定义的都是静态方法. Collections和Collection有什么差别? Co ...

  4. Dan计划:重新定义人生的10000个小时 - 阮一峰的网络日志

    Dan计划:重新定义人生的10000个小时 - 阮一峰的网络日志 Dan计划:重新定义人生的10000个小时

  5. 【VBA研究】查找目录以下全部文件的名称

    作者:iamlaosong 目录里面保存有面单扫描的图像文件,文件名称为邮件号码.如今想收集这些邮件号码,由于量非常大,不可能一个一个的截取,仅仅能通过程序实现.假定,当前工作表B列里放的是存放这些图 ...

  6. hdu1160(最长上升子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意:给定一些小猫的属性:体重和速度.然后求某些猫顺序的排列,使得体重上升,速度下降,这样的排列 ...

  7. COCO-Android开发框架公布

    一. COCO-Android说明 二. COCO-Android结构图 三. COCOBuild 四. COCOFrame 一.COCO-Android说明 1. COCO-Android是支撑An ...

  8. SqlServer操作远程数据库

    exec sp_addlinkedserver 'srv2','','mssql2008','服务器IP' exec sp_addlinkedsrvlogin 'srv2','false',null, ...

  9. SocketAsyncEventArgs使用解说

    原文:SocketAsyncEventArgs使用解说 如果在.NET下写过网络通讯的同学应该感觉不陌生了,有很多刚入门的同学很多都认为这东西可以大大提高处理效能还有就是使用上很不适应.其实使用之前最 ...

  10. oracle的to_char中的fm

    SQL> select '|'||to_char(5,'999')||'|' from dual;  结果为:|   5| SQL> select '|'||to_char(5,'000' ...