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)的更多相关文章

  1. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  2. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  3. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  4. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  5. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  6. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  7. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  8. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. Mastering Web Application Development with AngularJS 读书笔记(一)

    第一章笔记 (一) 一.PS:运行时配置IIS <html> <head> <script src="angular.js"></scri ...

  2. Linux鲜为人知的安全漏洞:不要将输出内容管道给你的shell

    将wget或curl输出的内容管道给bash或者sh是一件非常愚蠢的事,例如像下面这样: wget -O - http://example.com/install.sh | sudo sh 命令解释: ...

  3. VTK初学一,a Mesh from vtkImageData—球冠

    #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...

  4. jquery Ajax跨域调用WebServices方法

    由于公司需要开发一个手机页面,想提供给同事直接在手机上可以查询SAP资料.数据需要使用js调用webserver来获取. 因为初次使用Jquery调用Webserver,所以期间并不顺利.测试调用We ...

  5. 清北暑假模拟day1 艳阳天

    /* 注意P有可能不是质数,不要用欧拉函数那一套,正解可以倍增,就是等比数列和的性质,注意n是否为奇数 */ #include <cstdio> #include <algorith ...

  6. 调用WebService 实现在线双向翻译

    >先了解一下Web Service的基本概念: Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传 ...

  7. HDOJ 3709 Balanced Number

    数位DP... Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java ...

  8. Hibernate框架之入门案例

    今天终于开始学习了三大框架的其中一个框架,Hibernate框架,在这里不去讲Hibernate框架的一些基础概念了,直接切入代码,带大家了解一下Hibernate能干什么, Hibernate的人们 ...

  9. java 自定义标签 传值

    <?xml version="1.0" encoding="UTF-8" ?>   <taglib xmlns="http://ja ...

  10. 访问者(Visitor)模式

    http://www.cnblogs.com/zhenyulu/articles/79719.html 一. 访问者(Visitor)模式 访问者模式的目的是封装一些施加于某种数据结构元素之上的操作. ...