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 ...
随机推荐
- [PKUWC2018]随机算法
题意:https://loj.ac/problem/2540 给定一个图(n<=20),定义一个求最大独立集的随机化算法 产生一个排列,依次加入,能加入就加入 求得到最大独立集的概率 loj25 ...
- JQuery学习二(获取元素控件并控制)
$(’#id‘).+function; 例如: 1 <head> 2 <title>JQuery</title> 3 <script src="js ...
- 旋转 3d
建议chorme浏览器浏览,有样式兼容性问题. 图片可以根据自己本地路径设置路径,js库引用jquery. 写的不好,多多建议,谢谢大家. <html onselectstart="r ...
- [Jenkins 新插件] 兼容阿里开发手册 (P3C) 的火线插件安装使用教程
一.前言 火线(Fireline)的Jenkins官方插件已经上线,目前火线不仅能检查出安卓代码中的安全类问题和内存泄露问题,还兼容了阿里开源的Java开发规约(P3C项目),本文将以教程的形式帮助大 ...
- elasticsearch client
你可以用client做很多事情: 在集群中执行index, get, delete, search,update 操作 在集群中执行administrative tasks 如果你想再程序中嵌入ela ...
- jsp05 指令与动作
JSP7个动作指令如下 : jsp:forward: 执行页面转向,将请求的处理转发到下一个页面. jsp:param: 用于传递参数,必须与其他支持参数曲标签一起使用. jsp:include: 用 ...
- java collection(一)
1.Collection层次结构: 2.集合Conllection的基本概念: (1)集合的基本认识:如StringBuffer&StringBuilder是集合(存储的对象类型是String ...
- 图连通性【tarjan点双连通分量、边双联通分量】【无向图】
根据 李煜东大牛:图连通性若干拓展问题探讨 ppt学习. 有割点不一定有割边,有割边不一定有割点. 理解low[u]的定义很重要. 1.无向图求割点.点双联通分量: 如果对一条边(x,y),如果low ...
- Android Studio注意事项
http://www.android-studio.org/ 解决方法: 在 Android Studio 安装目录 bin/idea.properties 文件最后追加一句 1 disable.an ...
- ThinkSnS v4后台任意文件下载漏洞
漏洞文件: /apps/admin/Lib/Action/UpgradeAction.class.php 主要问题还是出现在了180行直接将远程获取到的图片直接保存. 文中可见并没有做任何的对$dow ...