题目

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. css小知识之伪元素

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  2. 321android浏览器

    [应用简介] 321浏览器是IT蓝豹创始人在2012年的时候业余时间研发的项目,现在很多各大网站都扒了我们的源码, 并且还插入了他们的广告,这是一件特别令人痛恨的事,如果需要下载整版的同学们就上IT蓝 ...

  3. ORM即 对象-关系映射(转自:微冷的雨)

    ORM即 对象-关系映射: 将数据库中的数据关系表,映射为实体对象. 灵动思绪EF(Entity FrameWork) 作者: 微冷的雨  来源: 博客园  发布时间: 2013-01-22 16:2 ...

  4. unity3d 孤岛求生基础案例

    第二个案例,此案例主要实现了第一人称控制器,把移动从世界坐标系转化到人物平面坐标系,通过碰撞器,触发器,光线透射触发器实现交互.实现UI texture记录收集信息,ui texture是更新内容对应 ...

  5. Web 项目中分享到微博、QQ空间等分享功能

    Web 项目中分享到微博.QQ空间等分享功能 网上有很多的模板以及代码,但是有很多都不能分享内容,简单的测试了下: 以新浪微博为例,文本框中的内容是title属性,下面的链接是url属性,如果你的链接 ...

  6. jenkins入门

    look at http://www.cnblogs.com/itech/archive/2011/11/23/2260009.html

  7. jsp js action之间的传值

    1.struts2 action如何向JSP的JS函数传值 action中定义变量public class TestAction extends ActionSupport implements Se ...

  8. wcf用svcutil导出泛型的元数据

    D:\aaa>svcutil net.tcp://192.168.1.110:44444/TradingsService.svc/mex /ct:System.Collections.Gener ...

  9. python第十八天-----Django基础

    1.路由系统 a.普通路由 url(r'^index$', views.index), b.正则路由 url(r'^index/(\d*)', views.index), url(r'^manage/ ...

  10. ajax将json写到table中去

    查询条件: <table style="width: 100%;border-collapse: collapse;" > <tr> <th styl ...