LeetCode OJ-- Distinct Subsequences ** 递推
https://oj.leetcode.com/problems/distinct-subsequences/
对于string S 和 T求,T 是 S的几种子串。
首先想到了递归方法,列出递归公式,奈何超时了:
如果S[sBegin] == T[tBegin] 则是 numSubDistinct(sBegin+1,tBegin+1,S,T) + numSubDistinct(sBegin+1,tBegin,S,T);
如果不等于,则 numSubDistinct(sBegin+1,tBegin,S,T);
如果T的遍历指针到头了,则说明成功了一次匹配
class Solution {
public:
int numDistinct(string S, string T) {
int ans = ;
if(S.size()<T.size())
return ;
if(S.size() == T.size())
if(S == T)
return ;
else
return ;
return numSubDistinct(,,S,T);
}
int numSubDistinct(int sBegin,int tBegin,string &S,string &T)
{
if(tBegin== T.size())
return ;
if(sBegin == S.size() && tBegin!= T.size())
return ;
if(S[sBegin] == T[tBegin])
return numSubDistinct(sBegin+,tBegin+,S,T) + numSubDistinct(sBegin+,tBegin,S,T);
else
return numSubDistinct(sBegin+,tBegin,S,T);
}
};
之后,考虑用递推实现。
根据思路来说,首先想到从后往前递推,但如果从后往前可以,则从前往后也可以。
建立二维数组num[S.size()][T.size()]存这个过程中产生的中间结果。
首先定义:num[i][j]是对于S从0到 i 的子串,对于T从 0 到 j 的子串,这两个字符串,T是S的子串数。
如果S[sBegin] == T[tBegin] num[sBegin][tBegin] = num[sBegin-1][tBegin-1] + num[sBegin-1][tBegin];
如果不等于则 num[sBegin][tBegin] = num[sBegin-1][tBegin];
根据公式,考虑到进行两层循环,
但是循环之前需要初始化
class Solution {
public:
int numDistinct(string S, string T) {
int ans = ;
if(S.size()<T.size())
return ;
if(S.size() == T.size())
if(S == T)
return ;
else
return ; vector<vector<int> > num;
num.resize(S.size());
for(int i = ;i<S.size();i++)
num[i].resize(T.size()); //initialize
if(S[] == T[])
num[][] = ;
else
num[][] = ;
for(int i = ;i<S.size();i++)
{
if(S[i] == T[])
num[i][] = num[i-][] + ;
else
num[i][] = num[i-][];
} for(int j = ;j<T.size();j++)
{
num[][j] = ;
} for(int sBegin = ;sBegin<S.size();sBegin++)
for(int tBegin = ;tBegin<T.size();tBegin++)
{
if(S[sBegin] == T[tBegin])
num[sBegin][tBegin] = num[sBegin-][tBegin-] + num[sBegin-][tBegin];
else
num[sBegin][tBegin] = num[sBegin-][tBegin];
} return num[S.size()-][T.size()-];
}
};
LeetCode OJ-- Distinct Subsequences ** 递推的更多相关文章
- [LeetCode OJ] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [LeetCode] 115. Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- Java for LeetCode 115 Distinct Subsequences【HARD】
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【leetcode】Distinct Subsequences(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- leetcode 115 Distinct Subsequences ----- java
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode 70 - 爬楼梯 - [递推+滚动优化]
假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2输出: 2解释: 有两种方 ...
- [leetcode]115. Distinct Subsequences 计算不同子序列个数
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- 九度OJ 1081:递推数列 (递归,二分法)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6194 解决:864 题目描述: 给定a0,a1,以及an=p*a(n-1) + q*a(n-2)中的p,q.这里n >= 2. 求第 ...
随机推荐
- Ubuntu12.04下YouCompleteMe安装教程(部分)
1.通过源码编译安装VIM 开发中使用的是Ubuntu 12.04 LTS,通过sudo apt-get install vim安装的版本较低,不支持YCM,所以,用源码编译并安装最新的Vim. 卸载 ...
- 2017 United Kingdom and Ireland Programming(Gym - 101606)
题目很水.睡过了迟到了一个小时,到达战场一看,俩队友AC五个了.. 就只贴我补的几个吧. B - Breaking Biscuits Gym - 101606B 旋转卡壳模板题.然后敲错了. 代码是另 ...
- Spring核心技术(十四)——ApplicationContext的额外功能
在前文的介绍中我们知道,org.springframework.beans.factory包提供了一些基本的功能来管理和控制Bean,甚至通过编程的方式来实现.org.springframework. ...
- Java语言基础---逻辑运算(长路短路运算)
长路短路运算的区别 长路与运算&:是指在两边都是整数时,是逐位与运算,在两边是关系运算时,是逻辑运算. 短路与运算&&:是指从左至右,遇到false,则停止后面的运算. 长路或 ...
- 自己用C语言写RH850 F1KM serial bootloader
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 手上有块Renesas ...
- play后面加the不加the如何分辨
play表示“参加(某种球类运动或棋牌类的活动)”时,不需要定冠词the,后面直接加球类运动名称或棋牌类活动名称,可根据实际情况翻译成“打,踢,下”等.例如: 1) He often plays fo ...
- mysql 分类
一.系统变量 说明:变量由系统提供,不用自定义 语法: 1.查看系统变量 show[global | session]varisables like ‘ ’:如果没有显示声明global 还是sess ...
- 洛谷P1540 机器翻译
题目链接:https://www.luogu.org/problemnew/show/P1540
- day18 js 正则,UI框架,Django helloworld 以及完整工作流程
JS正则: text 判断字符串是否符合规定的正则表达式 exec 获取匹配的数据 默认情况下: 只要能匹配到就返回true 否则返回false 只匹配数字: 所以J ...
- Apache Compress-使用
Apache Compress 是什么? Apache 提供的文件压缩工具. 运行环境 jdk 1.7 commons-compress 1.15 测试代码 package com.m.basic; ...