【leetcode】Distinct Subsequences(hard)
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"
意思是指可以通过多少种方式删除S的一部分字母使得S变为T。
思路:真高兴,又做出来了~~
用ways[m][n]存储 S[0~m-1]变为T[0~n-1]的方式
那么 ways[i][j] = ways[i-1][j] //扔掉S[i-1]
+((S[i-1] == T[j-1]) ? ways[i-1][j-1] : 0); //当前S与T的字母匹配,则需加上S[0~m-2]变为T[0~n-2]的方式数
class Solution {
public:
int numDistinct(string S, string T) {
int slen = S.length();
int tlen = T.length();
if(slen < tlen) return ;
vector<vector<int>> ways(slen + , vector<int>(tlen + , ));
ways[][] = ;
for(int i = ; i < slen + ; i++)
{
ways[i][] = ; //若T没有字母那么只有一种方式令S变为T:删除S全部的字母
}
for(int i = ; i < slen + ; i++)
{
for(int j = ; j < tlen + ; j++)
{
ways[i][j] = ways[i-][j] //扔掉当前的
+((S[i-] == T[j-]) ? ways[i-][j-] : ); //当前S与T的字母匹配
}
}
return ways[slen][tlen];
}
};
看了看别人的答案,发现我们的代码几乎是一模一样,难道说题做多了大家的风格都一样了吗?
有个优化的方法,因为在计算ways[i][j]时,只用到了ways[i-1]行的信息,所以没有必要存储所有的历史信息,只要存上一行的就好。
/**
* Further optimization could be made that we can use only 1D array instead of a
* matrix, since we only need data from last time step.
*/ int numDistinct(string S, string T) {
int m = T.length();
int n = S.length();
if (m > n) return ; // impossible for subsequence vector<int> path(m+, );
path[] = ; // initial condition for (int j = ; j <= n; j++) {
// traversing backwards so we are using path[i-1] from last time step
for (int i = m; i >= ; i--) {
path[i] = path[i] + (T[i-] == S[j-] ? path[i-] : );
}
} return path[m];
}
【leetcode】Distinct Subsequences(hard)的更多相关文章
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】深搜DFS(共85题)
[98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- 使用jasmine来对js进行单元测试
互联网的快速发展,给web开发人员带来了前所未有的挑战.对于前端开发,前端开发er所需要编写的js早已不是那些寥寥几行的视觉效果代码.代码量的大增,多人协同,人员素质悬殊不齐,这都需要一个标准,来对代 ...
- Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
方案一:用Eclipse自带的Export功能 步骤1:准备主清单文件 “MANIFEST.MF”, 由于是打包引用了第三方jar包的Java项目,故需要自定义配置文件MANIFEST.MF,在该项目 ...
- 此请求的查询字符串的长度超过配置的 maxQueryStringLength 值 --不仅wen.fonfig一个地方需要设置
提示已经很明确了... 搜出来的都是: <system.webServer> <security> <requestFiltering> <requestLi ...
- Outlook不能打开附件(提示:无法创建文件xx,请右键单击要在其中创建文件的文件夹..)
问题分析: 出现这种问题的几率很小,除非你是每天都需要使用Outlook的办公人员.出现这种问题我想有如下两种可能.1.注册表中指定的附档临时保存的目录没有写入的相关权限.2.同名附档已存在且权限出现 ...
- 01什么是ExtJs?
前言: 我之前是搞EasyUI+KO.js的,由于最近项目需要用到ExtJs.因此.边学边记录我的学习历程,希望能给自己和大家一点帮助. 1.0什么是ExtJs? 首先,什么是ExtJs呢?ExtJs ...
- UOJ#35 —— 后缀排序
1.题目大意:后缀数组模板题 2.分析:汝佳的书上的代码的有bug,还有那个n是字符串长度+1,''也要加入排序的 存个模板QAQ #include <cstdio> #include & ...
- js计算24点
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 2.实现Express中间件
Express提供的大部分功能都是通过中间件函数完成,这些中间件函数在Node.js收到 请求的时点 和 发送响应的时点 执行 connect模块提供了中间件框剪 方便在全局或路径级别或为单个路由插入 ...
- osc的git服务器简单使用。
1.创建本地仓库 mkdir dir cd dir git init 初始化仓库 2.创建远程仓库 进入远程仓库去创建 3.关联本地仓库与远程仓库 git remote add osc git::.. ...
- django-cms安装
ubuntu:12.04 (32bit) djangocms 0.5.1 =========================== 首先,跟着这个做: https://github.com/divio/ ...