LeetCode之“动态规划”: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
.
该题解析参考自LeetCode题解。
设状态为dp[i][j],表示T[0, j]在S[0, i]里出现的次数。首先,无论S[i]和T[j]是否相等,若不使用S[i],则dp[i][j]=dp[i-1][j];若S[i]=T[j],则可以使用S[i],此时dp[i][j]=dp[i-1][j]+dp[i-1][j-1]。
代码如下:
class Solution {
public:
int numDistinct(string s, string t) {
int szS = s.size();
int szT = t.size();
if(szS < szT)
return ; vector<vector<int> > dp(szS + , vector<int>(szT + , ));
for(int i = ; i < szS + ; i++)
dp[i][] = ; for(int i = ; i < szS + ; i++)
for(int j = ; j < szT + ; j++)
{
if(s[i-] != t[j-])
dp[i][j] = dp[i-][j];
else
dp[i][j] = dp[i-][j] + dp[i-][j-];
} return dp[szS][szT];
}
};
LeetCode之“动态规划”:Distinct Subsequences的更多相关文章
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】940. Distinct Subsequences II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
- leetcode -day 15 Distinct Subsequences
1. Distinct Subsequences Given a string S and a string T, count the number of distinct subsequen ...
- 【LeetCode】114. Distinct Subsequences
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- 【leetcode】940. Distinct Subsequences II
题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...
- 动态规划——Distinct Subsequences
题目大意:给定字符串S和T,现在从S中任选字符组成T,要求输出方案个数. Example 1:Input: S = "rabbbit", T = "rabbit" ...
- 【Leetcode】115. Distinct Subsequences
Description: Given two string S and T, you need to count the number of T's subsequences appeared in ...
随机推荐
- Basic Git commands
Basic Git commands Skip to end of metadata Created by Paul Watson [Atlassian], last modified on Nov ...
- introduction of velocity
一.velocity 简介 基于java 的模板引擎,apache 旗下的开源软件项目. 目的在于隔离 表示层和业务逻辑层,当然现在做的不仅仅是这些. 二.应用场景 web 应用程序:创建html页面 ...
- Swift延迟加载的一种用途
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 有以下一种情况: 我们试图用Cocoa的语音合成类NSSpee ...
- FFmpeg源代码简单分析:avcodec_encode_video()
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- Android存储之SharedPreferences
Android数据存储之SharedPreferences SharedPreferences对象初始化 SharedPreferences mSharedPreferences = getShare ...
- Dynamics CRM 插件Plugin中获取和更新时间字段值的准确转换
前面两篇介绍了后台代码通过组织服务获取更新时间字段.窗体javascript通过Odata获取更新时间字段,最后篇来实验下在插件中的获取和更新时间字段是否需要时制的转化,为何说是最后篇呢,因为在CRM ...
- Android初级教程:Android中解析方式之pull解析
在安卓中有很多种解析方式.按照大方向有xml解析和json解析.而,细致的分,xml和json解析各有自己的很多解析方式.今天这一篇主要介绍xml解析中的pull解析.对于xml的解析方式,我之前在j ...
- 程序员必须搞清的概念-equals和=和hashcode的区别
1. 首先equals()和hashcode的介绍 equals 方法在非空对象引用上实现相等关系: * 自反性:对于任何非空引用值 x,x.equals(x) 都应返回 true. * 对称性:对于 ...
- 修改Chrome默认的搜索引擎
Chrome浏览器可谓是现在世界上最受欢迎的一款浏览器了,不仅仅是在其简单易用还有优雅的界面,还有与一众以人为本的设计理念,在我的眼里,匠心独具.但是咧,由于在国内谷歌是被禁止访问的,所以Chrome ...
- iOS中 Realm错误总结整理 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 一.错误信息:Attempting to modify object outside of a write tra ...