LeetCode: Distinct Subsequences [115]
【称号】
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]的更多相关文章
- 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)
引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [leetcode]Distinct Subsequences @ Python
原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [LeetCode] Distinct Subsequences 解题思路
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode: Distinct Subsequences 解题报告
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- [LeetCode] Distinct Subsequences [29]
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode] distinct subsequences 不同子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [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 ...
随机推荐
- android画笔错位问题的解决
下面的画画板的代码: public class MainActivity extends Activity { private ImageView iv; private Bitmap baseBit ...
- EA强大功能之代码凝视
前面讲了EA怎样方便我们生成代码,这次讲一下,怎样生成具体的凝视. 1.文件表头凝视 (1)点击工具----选项 在常规项里改动作者: 在代码project中改动代码project的默认语言. (2) ...
- _tcscat在Debug和Release根据问题
背景: 因此,例如,在下面的代码段,作用是得到的路径当前程序(C:\work\A.exe),然后"A.exe"拆除,组装的"C:\work\inject.dll" ...
- hdu4289(最小割)
传送门:Control 题意:有n个城市,有个小偷想从其中一个城市逃到另一个城市,警察想要堵截这个小偷,知道了在每个城市堵截的成本,问如何安排在哪些城市堵截可以使得小偷一定会被抓住,而且成本最低. 分 ...
- hdu1428之dfs+spfa
漫步校园 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- BroadcastReceiver总结
一.工程整体图 二.activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ ...
- JAVA实现Shell排序
Shell排序可以理解为插入排序的变种,它充分利用了插入排序的两个特点: (1). 当数据规模小的时候非常高效. (2). 当给定数据已经有序时的时间代价为O(N) 所以,Shell排序每次把数据分成 ...
- Codeforces Round #248 (Div. 1)——Ryouko's Memory Note
题目连接 题意: 给n和m,一行m个1<=x<=n的数.记c=.如今仅仅能选择一个数x变成y,序列中全部等于x的值都变成y,求最小的c 分析: 对于一个数x,把与他相邻的所有的非x的数所有 ...
- Cocos2d-x精华教程汇总(第三期) cocos2d-x最新离线API文档下载(最新版3.6更新。。。)
其实使用doxygen在Cocos2d-x引擎的doc目录下可以生成离线文档,但是可能每个人为了生成一个离线文档去安装甚至编译doxygen毕竟麻烦,而且现有的doxygen无法生成多语言版本的离线文 ...
- ServiceProvider实现
ServiceProvider实现揭秘 [总体设计 ] 本系列前面的文章我们主要以编程的角度对ASP.NET Core的依赖注入系统进行了详细的介绍,如果读者朋友们对这些内容具有深刻的理解,我相信你们 ...