题目

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.

分析

题目给定两个字符串,选择只可以用删除字符的方法从第一个字符串变换到第二个字符串,求出一共有多少种变换方法;
又是动态规划思想应用的典型题目。
  • 定义二维数组dp[i][j]为字符串s(0,i)变换到t(0,j)的变换方法。
  • 如果S[i]==T[j],那么dp[i][j] = dp[i-1][j-1] + dp[i-1][j]。意思是:如果当前S[i]==T[j],那么当前这个字母即可以保留也可以抛弃,所以变换方法等于保留这个字母的变换方法加上不用这个字母的变换方法。
  • 如果S[i]!=T[i],那么dp[i][j] = dp[i-1][j],意思是如果当前字符不等,那么就只能抛弃当前这个字符。
  • 递归公式中用到的dp[0][0] = 1,dp[i][0] = 0(把任意一个字符串变换为一个空串只有一个方法)

AC代码

 class Solution {
public:
/*用删除的方法将串s变换到t,计算变换方法数*/
int numDistinct(string s, string t) {
if (s.empty() || t.empty())
return ;
else if (s.length() < t.length())
return ;
else
{
//动态规划
int ls = s.length(), lt = t.length();
/*保存由字符串s(0,i) --> t(0,j)的方法数*/
vector<vector<int> > dp(ls + , vector<int>(lt + , ));
dp[][] = ;
for (int i = ; i < ls; ++i)
{
/*s(0,i) 转换为 t(0)的方法数为1*/
dp[i][] = ;
}//for
for (int i = ; i <= ls; ++i)
{
for (int j = ; j <= lt; ++j)
{
/*首先不管当前字符是否相同,为dp[i][j]赋初值*/
dp[i][j] = dp[i - ][j];
if (s[i-] == t[j-])
{
/*如果s和t的当前字符相同,有两种选择保留或不保留*/
dp[i][j] += dp[i - ][j - ];
}//if
}//for
}//for
return dp[ls][lt];
}
}
};

GitHub测试程序源码

LeetCode(115) Distinct Subsequences的更多相关文章

  1. LeetCode(115):不同的子序列

    Hard! 题目描述: 给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字 ...

  2. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  3. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  4. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  5. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  6. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  7. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  8. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  9. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

随机推荐

  1. [转载]我的WCF之旅(1):创建一个简单的WCF程序

    为了使读者对基于WCF的编程模型有一个直观的映像,我将带领读者一步一步地创建一个完整的WCF应用.本应用功能虽然简单,但它涵盖了一个完整WCF应用的基本结构.对那些对WCF不是很了解的读者来说,这个例 ...

  2. Generate SQL from Excel

    Tips:   SUBSTITUTE(D4,"'","''")---if D4 contain ', this function will convert ' ...

  3. winform开发之UI系列

    1.如何构造一个漂亮的主窗体 主要讲述如何对一个新建窗体的美化过程,涉及到经常需要用到的几个属性我会着重强调它的用法,并不断更新它,因为楼主也正在探索中.... 步骤如下: vs新建一个winform ...

  4. NOR Flash擦写和原理分析

    NOR Flash擦写和原理分析 1. NOR FLASH 的简单介绍 NOR FLASH 是很常见的一种存储芯片,数据掉电不会丢失.NOR FLASH支持Execute On Chip,即程序可以直 ...

  5. ajax回调打开新窗体防止浏览器拦截有效方法

    ajax回调打开新窗体防止浏览器拦截,就这么做! 问题剖析:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 function click_fun(){    window ...

  6. [原]通过配合ffmpeg.exe获取视频文件时长

    import subprocess import os import time def getTime(flvpath,fid): #file_str = '1.flv' file_str = flv ...

  7. Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式总结

    最近老是遇见服务器内存溢出的问题,故在网上搜了搜,总结了一些java内存溢出的解决方式 java.lang.OutOfMemoryError这个错误我相信大部分开发人员都有遇到过,产生该错误的原因大都 ...

  8. PTA Insert or Merge

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  9. Postgres SQL学习笔记

    1.创建表,添加,查询 create table StuInfo ( StuId SERIAL primary key,-- 自动增长列 StuName ), Birthday Date, StuPh ...

  10. iOS常用库之Masonry

    简单介绍 Masonry 源码地址:https://github.com/Masonry/Masonry Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简 ...