原题地址

转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的

举个例子,S="aabb",T="ab"。构造如下地图("."表示空位,"^"表示起点,"$"表示终点),我们的目标就是求从起点到终点一共有多少条路径。

  a  b
a ^ .
a . .
b . .
b . $

对于任意一个位置,不妨设坐标为(i, j),则有:如果S[i]等于T[j],可以向下走也可以向右下走;否则只能向下走

设count[i][j]表示从(i, j)开始的走法,则count[i][j] = count[i+1][j](如果S[i]不等于T[j]) 或 count[i+1][j] + count[i+1][j+1](如果S[i]等于T[j])。

由于求count[i][j]只用到了count[i+1][X],所以在具体实现的时候可以用一维数组压缩存储状态空间。

代码:

 int numDistinct(string S, string T) {
int slen = S.length();
int tlen = T.length();
vector<int> count(tlen + , ); count[tlen] = ; for (int i = slen - ; i >= ; i--)
for (int j = ; j < tlen; j++)
count[j] += S[i] == T[j] ? count[j + ] : ; return count[];
}

Leetcode#115 Distinct Subsequences的更多相关文章

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

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

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

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

  5. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

  6. 【一天一道LeetCode】#115. Distinct Subsequences

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. 【LeetCode】115. Distinct Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  8. 115. Distinct Subsequences

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  9. [Leetcode][JAVA] Distinct Subsequences

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

随机推荐

  1. php中empty(), is_null(), isset()函数区别

    empty(), is_null(), isset()真值表(区别) 我们先来看看这3个函数的功能描述 www.111cn.net isset 判断变量是否已存在,如果变量存在则返回 TRUE,否则返 ...

  2. SQL Server自增长列插入指定值 -- SET IDENTITY_INSERT ON|OFF(转)

    想要将值插入到自动编号(或者说是标识列,IDENTITY)中去,需要设定 SET IDENTITY_INSERT 示例: 1.首先建立一个有标识列的表:CREATE TABLE products (i ...

  3. C#导出GridView数据到Excel文件类

    using System; using System.Web; using System.Web.UI; using System.IO; using System.Web.UI.WebControl ...

  4. 从客户端检测到有潜在危险的Request.Form 值【转】

    asp.net开发中,经常遇到“从客户端检测到有潜在危险的Request.Form 值”错误提示,很多人给出的解决方案是: 1.web.config文档<system.web>后面加入这一 ...

  5. python学习应用笔记(一)

    之前一直用c++写程序  所以考虑程序一般都比较容易往数据结构的方向想 而自己设计数据结构往往要费很大事  昨天看了一下python  发现脚本语言 真是厉害    用来进行模拟运算确实不错  可以先 ...

  6. C 封装一个简单二叉树基库

    引文 今天分享一个喜欢佩服的伟人,应该算人类文明极大突破者.收藏过一张纸币类型如下 那我们继续科普一段关于他的简介 '高斯有些孤傲,但令人惊奇的是,他春风得意地度过了中产阶级的一生,而  没有遭受到冷 ...

  7. 自己的php函数库

    //判断数组中是否有元素为空的函数,支持多维数组,相似系统函数in_array(value,array,type) function is_null_array($arr) { if(!is_arra ...

  8. oracle11g 重新配置em

    OS: ORACLE-LINUX 5.7 DB: 11.2.0.3 [oracle@b28-122 ~]$ emctl status dbconsoleOracle Enterprise Manage ...

  9. Tomcat 服务器服务的注册修改删除

    1. 注册Tomcat服务 运行cmd,切换目录到tomcat/bin, 执行以下命令service.bat install 2.删除Tomcat服务

  10. qtp 设置等待时间

    1.file->settings->run .默认的时间是20 秒 2. browser("browser").Navigate http://www.baidu.co ...