【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 ...
随机推荐
- PHP CLI下接受参数的几种方法
PHP CLI(命令行模式下)接受参数有多种方法: (1)使用$argv接受参数 <?php //变量仅在 register_argc_argv 打开时可用. print_r($argc); / ...
- sql拼音简写函数
USE [HotelDB]GO /****** Object: UserDefinedFunction [dbo].[fn_GetPy] Script Date: 2016/1/4 13:29:13 ...
- nyoj 252 01串 动态规划( java)
当n=2时, 输出 3:当n=3时, 输出 5:当n=4时, 输出 8: #### 分析: 当n=4时,如 0101 符合条件, 当第一个位置是0时,还剩3个位置 ,与n=3时个数相等: 符合条件的为 ...
- hash-4.hashtable
1.先看hashtable的源代码 public synchronized V put(K key, V value) { // Make sure the value is not null if ...
- git之install
一.window安装 1.下载路径 https://git-for-windows.github.io/ 2.如何在windows下安装GIT_百度经验 3.做完上面两部打开Git bash即可执行g ...
- 字符串匹配算法——KMP算法
处理字符串的过程中,难免会遇到字符匹配的问题.常用的字符匹配方法 1. 朴素模式匹配算法(Brute-Force算法) 求子串位置的定位函数Index( S, T, pos). 模式匹配:子串的定位操 ...
- 吉他笔记 solo 和弦 推弦 音程
十二平均律: 如下图所示: 第一行为唱名:do re mi fa so.... 第二行为音名:C #C D #D E F #F G #G A #A B C 第三行为D调对应的音名,即1 = D 第四行 ...
- 信号屏蔽的切换的理解sigsuspend
#include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h&g ...
- HNU 12869 Sequence(循环节)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12869 解题报告:看到n的范围这么大,一看就是找规律,把前30 ...
- iOS开发——UI基础-提示框
提示框的种类有很多,废话不多说,直接上代码 一.文本提示框 运行结果如下: 代码实现如下: @interface ViewController () // 添加方法 - (IBAction)add; ...