LeetCode(115) 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.
分析
- 定义二维数组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];
}
}
};
LeetCode(115) Distinct Subsequences的更多相关文章
- LeetCode(115):不同的子序列
Hard! 题目描述: 给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字 ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- 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 ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Mac PHP 环境配置+渗透环境
/* 最近购入一台mac pro 配置自己的php环境以及渗透环境 留个笔记 */ Mac 自带的PHP 加 apache 所以就不需要install 咯.只需要down个mysql 就好了. 0x1 ...
- Hibernate和Struts2整合的增、删、改、查
1. 新建一个Web项目,准备好jar包和配置文件. 2. web.xml文件中添加过滤器 <?xml version="1.0" encoding="UTF-8& ...
- 如何让table中td宽度固定
table中td会随着里面的内容伸缩,设置其width样式并没有效果.这个时候需要下面的CSS可以实现. 首先是设置table .table {table-layout:fixed;} 其次是td . ...
- Redis从基础命令到实战之字符串类型
字符串类型是Redis中最基本的数据类型,能存储任何形式的字符串和和二进制数据.本文以代码形式列举常用的操作命令,并在实践部分演示一个简单的商品管理功能,实现了通常使用关系型数据库开发的增改查功能,注 ...
- iOS 10开发NSAssert(断言)的使用
断言(NSAssert)的使用 字数1055 阅读3270 评论3 喜欢30 NSAssert()是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否属于Bug ...
- redis实现tomcat集群session共享
1.部署两个tomcat节点,使用nginx实现集群(见http://www.cnblogs.com/zhangzhi0556/articles/nginx.html): 2.redis安装(见h ...
- 修改订单的收货时间(MySQL)脚本
需求 下单时间为21:00-23:59,并且收货时间为第二天的订单,将这些订单的收货时间改为第三天: #!/bin/bash ###time:2016-04-26 ###This script is ...
- C语言乱谈(一) 20行代码生成BMP
在学习图形图像的过程中,最简单和常见的格式是BMP和PPM.下面将给出生成BMP的极度精简代码,然后讲解BMP格式. #include <stdio.h> #include <std ...
- HAProxy学习笔记
HAProxy:著名的负载均衡器,工作于用户空间的服务程序,其有两种工作模式: TCP mode:四层调度(模拟实现,依赖于socket进行通信) HTTP mode:七层调度 目前维护的稳定版本分支 ...
- JavaScript流程控制语句
一.JavaScript分支语句 -alert() 弹出警告对话框 -prompt() 弹出输入框 1.if(){}else{} 栗子: var num=prompt("请输入电话号码 ...