LeetCode之“动态规划”:Distinct Subsequences
题目要求:
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.
该题解析参考自LeetCode题解。
设状态为dp[i][j],表示T[0, j]在S[0, i]里出现的次数。首先,无论S[i]和T[j]是否相等,若不使用S[i],则dp[i][j]=dp[i-1][j];若S[i]=T[j],则可以使用S[i],此时dp[i][j]=dp[i-1][j]+dp[i-1][j-1]。
代码如下:
 class Solution {
 public:
     int numDistinct(string s, string t) {
         int szS = s.size();
         int szT = t.size();
         if(szS < szT)
             return ;
         vector<vector<int> > dp(szS + , vector<int>(szT + , ));
         for(int i = ; i < szS + ; i++)
             dp[i][] = ;
         for(int i = ; i < szS + ; i++)
             for(int j = ; j < szT + ; j++)
             {
                 if(s[i-] != t[j-])
                     dp[i][j] = dp[i-][j];
                 else
                     dp[i][j] = dp[i-][j] + dp[i-][j-];
             }
         return dp[szS][szT];
     }
 };
LeetCode之“动态规划”:Distinct Subsequences的更多相关文章
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
		Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ... 
- 【一天一道LeetCode】#115. Distinct Subsequences
		一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ... 
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ... 
- 【LeetCode】940. Distinct Subsequences II 解题报告(Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ... 
- 【LeetCode OJ】Distinct Subsequences
		Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ... 
- leetcode -day 15 Distinct Subsequences
		1.  Distinct Subsequences Given a string S and a string T, count the number of distinct subsequen ... 
- 【LeetCode】114. Distinct Subsequences
		Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ... 
- 【leetcode】940. Distinct Subsequences II
		题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ... 
- 动态规划——Distinct Subsequences
		题目大意:给定字符串S和T,现在从S中任选字符组成T,要求输出方案个数. Example 1:Input: S = "rabbbit", T = "rabbit" ... 
- 【Leetcode】115. Distinct Subsequences
		Description: Given two string S and T, you need to count the number of T's subsequences appeared in ... 
随机推荐
- sum,filter和map参数里面的玄机
			首先是sum函数. 最常见的用法似乎是: >>> sum([1,2,3]) 6 但其实这是默认首个元素是数字0.我们可以指定其他数字: >>> sum([1,2,3 ... 
- openJdk和sun Jdk区别和安装
			openJdk和sun jdk的区别 使用过LINUX的人都应该知道,在大多数LINUX发行版本里,内置或者通过软件源安装JDK的话,都是安装的OpenJDK, 那么到底什么是OpenJDK,它与SU ... 
- UNIX网络编程——ioctl 函数的用法详解
			1.介绍 Linux网络程序与内核交互的方法是通过ioctl来实现的,ioctl与网络协议栈进行交互,可得到网络接口的信息,网卡设备的映射属性和配置网络接口.并且还能够查看,修改,删除ARP高速缓存的 ... 
- 剑指Offer——滴滴笔试题+知识点总结
			剑指Offer--滴滴笔试题+知识点总结 情景回顾 时间:2016.9.18 15:00-17:00 地点:山东省网络环境智能计算技术重点实验室 事件:滴滴笔试 总体来说,滴滴笔试内容体量不算多, ... 
- Ubuntu15.10 安装OpenCV3.1
			wget https://sourceforge.net/projects/opencvlibrary/files/opencv-unix/3.1.0/opencv-3.1.0.zip/downloa ... 
- 剑指Offer——二叉树
			剑指Offer--二叉树 前言 数据结构通常是编程面试中考察的重点.在参加面试之前,应聘者需要熟练掌握链表.树.栈.队列和哈希表等数据结构,以及它们的操作.本片博文主要讲解二叉树操作的相关知识,主要包 ... 
- UNIX网络编程——处理服务器中大量的TIME_WAIT
			出现条件: 服务器主动关闭 短连接服务加剧 根据TCP协议定义的3次握手断开连接规定,发起socket主动关闭的一方 socket将进入TIME_WAIT状态,TIME_WAIT状态将持续2个MSL( ... 
- Android官方命令深入分析之Device Monitor
			Android Device Monitor是一个提供了图形化界面的可以对Android应用进行调试和分析的独立的工具.Monitor工具不需要IDE环境,比如Android Studio.包括以下工 ... 
- 工作中常用的Linux命令
			1.从其他机器拷贝文件夹 格式: scp -r 文件夹名 用户名@机器名:/路径 范例: scp -rsearch work@zjm-testing-ps23.zjm.baidu.com:/home/ ... 
- Leetcode_13_Roman to Integer
			本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41486885 通过本文你可能学到的知识如下: (1)理解本 ... 
