原题地址

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

举个例子,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. LotusPhp起步:经典的HelloWorld

    写了几篇LotusPhp,一直没有跑个程序,感觉好像步骤有点错,所以先上个经典的Demo,HelloWorld吧 先按推荐目录建好文件夹,如果懒的建,下面有下载的Demo包,解压就可以用,因为简单,也 ...

  2. 推荐个好东西swoole,php如虎添翼

    Swoole:PHP语言的异步.并行.高性能网络通信框架,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTask,消息队列 ...

  3. java作用域public ,private ,protected 及不写时的区别(转)

    在说明这四个关键字之前,我想就class之间的关系做一个简单的定 义,对于继承自己的class,base class可以认为他们都是自己的子 女,而对于和自己一个目录下的classes,认为都是自己的 ...

  4. java android 访问DELPHI 的DATASNAP

    最新版的DELPHI开发DATASNAP非常简单便捷,DataSnap的REST风格和对JSON的支持,使之成为服务器端开发的神器. 一.DATASNAP服务器中的方法: TServerMethods ...

  5. 并行编程之CountdownEvent的用法

    教程:http://blog.gkarch.com/threading/part5.html#the-parallel-class http://www.cnblogs.com/huangxinche ...

  6. PHP引用文件

    require: 可能多次执行的代码用require效率要稍高 require_once: 唯一区别是 PHP 会检查该文件是否已经被包含过,如果是则不会再次包含 include: 语句包含并运行指定 ...

  7. JTable的DefaultModel方法getValueAt(a,row)

    行和列都是从0开始索引的,而且不包括netbeans生成的表格头,而是从数据开始,否则就会报错

  8. Oracle Study Note : Tablespace and Data Files

    1.how to create a tablespace that employs the most common features create tablespace tb_name #create ...

  9. oracle11g 新特性 - rman自动备份控制文件延迟

    OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 -6 ...

  10. android 连续点击退出程序

    package com.test.twiceexit; import java.util.Timer; import android.app.Activity;import android.os.Bu ...