Distinct Subsequences ——动态规划
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.
题意解读:只可以用删除字符的方法从第一个字符串变换到第二个字符串,求出一共有多少种变换方法。
解题分析:dfs可以做,但大数据超时。
动态规划,定义dp[i][j]为字符串i变换到j的变换方法。
首先考虑S[i]!=T[j],这个好理解,因为不相等,所以考虑s中i这个元素和不考虑i这个元素结果是一样的,所以,dp[i][j] = dp[i-1][j],意思是如果当前字符不等,那么就只能抛弃当前这个字符。
接下来考虑S[i]==T[j](因为代码中下标从1开始,所以代码中是S[i-1]==T[j-1]),那么dp[i][j] = dp[i-1][j-1] + dp[i-1][j](这里可以理解为dp[i-1][j]+1,但是这里的1其实不是1,是之前出现过的结果,想想这里还是很好理解的。)。意思是:如果当前S[i]==T[j],那么当前这个字母即可以保留也可以抛弃,所以变换方法等于保留这个字母的变换方法加上不用这个字母的变换方法。
递归公式中用到的res[i][0] = 1(把任意一个字符串变换为一个空串只有一个方法)
class Solution {
public:
int numDistinct(string s, string t) {
//完全不能理解啊
int ls=s.size();
int lt=t.size();
if(lt==)
return ;
if(ls==)
return ;
vector<vector<int>> res(ls+,vector<int> (lt+,));
for(int i=;i<=ls;i++)
{
res[i][]=;
}
for(int i=;i<=ls;i++)
for(int j=;j<=lt;j++)
{
res[i][j]=res[i-][j];
if(s[i-]==t[j-])
res[i][j]=res[i-][j]+res[i-][j-];
}
return res[ls][lt];
}
};
Distinct Subsequences ——动态规划的更多相关文章
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- LeetCode之“动态规划”:Distinct Subsequences
题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...
- Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划
Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...
- [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(115) Distinct Subsequences
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 30. Distinct Subsequences
Distinct Subsequences OJ: https://oj.leetcode.com/problems/distinct-subsequences/ Given a string S a ...
- Distinct Subsequences——Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- printf函数用法小记
By francis_hao Aug 26,2017 C语言中printf函数是一个比较常用的函数,但是常用并不代表完全了解,本文翻译了printf的man手册,介绍了其全部功能(不包括ma ...
- Linux网络监控工具nethogs
Linux网络监控工具nethogs 标签: 监控工具linux 2015-12-17 22:06 448人阅读 评论(0) 收藏 举报 分类: linux(40) 版权声明:本文为博主原创文章, ...
- http中有关缓存相关的几个字段
转载自:http://blog.csdn.net/lifeibo/article/details/5979572 Expires.Cache-Control.Last-Modified. ETag是R ...
- maven中package和install命令的区别
mvn package 编译并打包工程,根据pom文件中packaging是jar还是war进行打包,会在项目的target目录下生成一个jar/war包. mvn install 打包并安装到mav ...
- 2015/9/4 Python基础(8):映射和集合类型
Python里唯一的映射类型是字典.映射类型对象里,hash值(key)和指向的对象(值)是一对多的关系.字典对象是可变的,这一点上很像列表,它也可以存储任意个数任意类型的Python对象,其中包括容 ...
- Ubuntu12.04 安装nginx和mongo过程
1.安装php和php-cgi apt-get install php5 php5-cgi 2.安装 nginx apt-get install nginx 3.安装 MongoDB apt-get ...
- netcore 使用log4net
1.Install Install-Package log4net 2. conifg 創建文件:log4net.config <?xml version="1.0" enc ...
- C# 生成系统唯一号
生成唯一号:思路,根据yymmddhhmmss+自增长号+唯一服务器号( SystemNo)生成唯一码,总长度19,例如:1509281204550000101. public class Uniqu ...
- 两小时快速构建微信小程序
小程序在2017年1月上线之初,被社会极力吹捧,刻意去将其制造为一个“风口”,透支其价值.但是在之后一个月里,石破天惊迅速归为沉寂.媒体又开始过度消费小程序,大谈其鸡肋之处. 个人认为小程序的一个分水 ...
- 打开Android系统安装APK的页面
//使用隐式意图开启安装APK的Activity Intent intent = new Intent("android.intent.action.VIEW"); intent. ...