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.

题意:给定两个字符串,字符串S中能有几个T的字符串。字符串是不改变字符的相对位置,只是删减S中一部分字符,形成T,返回删减方式的种类。

思路:动态规划。维护一个二维数组dp[T.size()+1][S.size()+1],其中dp[i][j],表示T中[0,i-1]区间的子字符串和S中[0,j-1]匹配的个数.这里要注意的是,二维数组的下标和T、S中下标的对应关系,二维数组的比字符串中在左侧和上侧多一行和一列,多得为T和S中有一个为空字符串时的情况。以S ="rabbbit", T ="rabbit"得出相应的dp数组,如下:

得出状态转移方程为:dp[i][j]=dp[i][j-1]+(T[i-1]==S[j-1]?dp[i-1][j-1]:0);  (注意字符串和数组之间的下标转换),所以整体的思路是,先赋值第一行和第一列,然后由状态方程得出其他值,代码如下:

 class Solution {
public:
int numDistinct(string S, string T)
{
int dp[S.size()+][T.size()+];
for(int i=;i<T.size()+;++i)
dp[][i]=;
for(int i=;i<S.size()+;++i)
dp[i][]=; for(int i=;i<T.size()+;i++)
{
for(int j=;j<S.size()+;j++)
{
dp[i][j]=dp[i][j-]+(T[i-]==S[j-]?dp[i-][j-]:);
}
}
return dp[T.size()][S.size()];
}
};

网友Code Gander只使用一个一维数组便实现了这个过程。代码如下:

 class Solution {
public:
int numDistinct(string S, string T)
{
if(T.size()==) return ;
if(S.size()==) return ; vector<int> dp(T.size()+,);
dp[]=; for(int i=;i<S.size()+;++i)
{
for(int j=T.size()-;j>=;j--)
{
dp[j+]=(S[i]==T[j]?dp[j]:)+dp[j+];
}
}
return dp[T.size()];
}
};

[Leetcode] distinct subsequences 不同子序列的更多相关文章

  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 解题思路

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

  4. LeetCode: Distinct Subsequences [115]

    [称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...

  5. [leetcode]Distinct Subsequences @ Python

    原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...

  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

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

  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] Increasing Subsequences 递增子序列

    Given an integer array, your task is to find all the different possible increasing subsequences of t ...

随机推荐

  1. PHP学习课程和培训方向学习路线分享

    php语言的优越性,集结了很多的开发爱好者,无论行业前景和个人发展来说,php正飞速的发展,php在不断兼容着类似closures和命名空间 等技术,同时兼顾性能和当下流行的框架.版本是7之后,一直在 ...

  2. Linux上面安装redis和简单使用

    一.安装,redis的官方的网址   https://redis.io/ 目前的最高的版本是4.0,我安装的是2.*的版本 1.下载源码,解压后编译源码. $ wget http://download ...

  3. plsql 连接数据库无法解析指定的连接标识符

    之前用plsql连接的时候一直出问题,报无法解析指定的连接标识符,但是我加上ip地址就可以连接上. 我百度了很久,有说如下图选择oracle home的,有说清空admin目录下的所有文件, 但是都不 ...

  4. STM32(4)——系统时钟和SysTick

    1.STM32的时钟系统 在STM32中,一共有5个时钟源,分别是HSI.HSE.LSI.LSE.PLL HSI是高速内部时钟,RC振荡器,频率为8MHz: HSE是高速外部时钟,可接石英/陶瓷谐振器 ...

  5. stm32--FatFs移植(SPIFlash)

    前言 硬件: 单片机:stm32f072CB,sram大小16k.(其他单片机只要sram>8k即可通用) SPIFlash:W25Q128FV,16Mbyte,单次擦除最小4k. 程序使用Ke ...

  6. linux redhat 打开防火墙中的某个端口

    服务器成功监听了一个端口(如 5500),但是外面连接不进来,telnet其端口不通,解决办法如下(在root用户下): $ /sbin/iptables -I INPUT -p tcp --dpor ...

  7. Android开发免费类库和工具集合

    用于Android开发的免费类库和工具集合,按目录分类. Action Bars ActionBarSherlock Extended ActionBar FadingActionBar GlassA ...

  8. Qt QPainter::end: Painter ended whith 2 saced states

    在使用Qt  QPainter 的时候,有时会遇到“QPainter::end: Painter ended whith 2 saced states” 这时由于我们在使用的QPanter.trans ...

  9. jmeter完成CAS登录,并获取token(原创)

    思路: 1.系统完成CAS登录需要验证用户名/密码,以及动态授权参数 2.先通过指定url用正则提取出动态授权参数 3.完成登录需要cookie,需用正则提取出对应的cookie,已完成参数化的自动登 ...

  10. 深度可分卷积(Depthwise Separable Conv.)计算量分析

    上次读到深度可分卷积还是去年暑假,各种细节都有些忘了.记录一下,特别是计算量的分析过程. 1. 标准卷积和深度可分卷积 标准卷积(MobileNet论文中称为Standard Convolution, ...