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 ** 递推的更多相关文章

  1. [LeetCode OJ] Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  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 ...

  3. 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 ...

  4. [Leetcode][JAVA] Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  5. 【leetcode】Distinct Subsequences(hard)

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  6. 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 ...

  7. LeetCode 70 - 爬楼梯 - [递推+滚动优化]

    假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2输出: 2解释: 有两种方 ...

  8. [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 ...

  9. 九度OJ 1081:递推数列 (递归,二分法)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6194 解决:864 题目描述: 给定a0,a1,以及an=p*a(n-1) + q*a(n-2)中的p,q.这里n >= 2. 求第 ...

随机推荐

  1. 精通SpringBoot:详解WebMvcConfigurer接口

    SpringBoot 确实为我们做了很多事情, 但有时候我们想要自己定义一些Handler,Interceptor,ViewResolver,MessageConverter,该怎么做呢.在Sprin ...

  2. 用iTerm快速链接远程服务器

    通常情况下,iTerm2访问远程Linux使用ssh ssh <用户名>@<ip> 然后输入访问的密码即可.当然还有的时候需要指定访问端口. ssh -p <端口号> ...

  3. Gender Equality in the Workplace【职场上的性别平等】

    Gender Equality in the Workplace A new batch of young women - members of the so-called Millennial ge ...

  4. Diycode开源项目 磁盘图片缓存+自定义webViewClient+图片点击js方法

    1.磁盘图片缓存器DiskImageCache 1.1.这个类很多情况都可能用的到,耦合性很低,所以分开讲. 源代码: /* * Copyright 2017 GcsSloop * * License ...

  5. Redis的概述、优势和安装部署

    Redis概述 Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使 ...

  6. hive操作语句

    设置属性: //设置本地执行作set hive.exec.mode.local.auto=true; //设置动态分区 set hive.exec.dynamic.partition=true; se ...

  7. JSP自定义tld方法标签

    卧槽 我们可以通过tld文件,自定义一个方法标签,以便在页面中使用,目录通常放在WEB-INF下面的tlds文件夹: 引入方式示例,直接在jsp上引入tld标签文件: <%@ taglib pr ...

  8. 报错: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: An attempt by a client to checkout a Connection has timed out. 数据库连接超时

    解决方法一: [oracle@data ~]$ sqlplus / as sysdba——连接到数据库 SQL*Plus: Release 11.2.0.4.0 Production on Mon M ...

  9. [转载]robotium脚本封装为APK,实现脱离手机数据线,使用按钮点击控制用例

    原文地址:robotium脚本封装为APK,实现脱离手机数据线,使用按钮点击控制用例运行作者:机器,猫 最近一直在完成一些robotium的小功能,用来更方便的完成一些小功能的测试,或者可以说用来娱乐 ...

  10. python 令人抓狂的编码问题

    #运行以下程序: #! /usr/bin/env python#coding=utf-8 file = open( 'all_hanzi.txt','wb' ) listhz = []n=0for c ...