原题地址

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

举个例子,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. mariadb主从复制架构学习笔记

    复制功用: 数据分布 负载均衡:读操作,适用于读密集型的应用 备份 高可用和故障切换 MySQL升级测试 在从服务器上有两个线程: I/O线程:从master请求二进制日志信息,并保存至中继日志 SQ ...

  2. php中intval()函数

    格式:int intval(mixed $var [, int $base]); 1.intval()的返回值是整型,1或者0.可作用于数组或者对象(对象报错信息:Notice: Object of ...

  3. RecyclerView中显示不同的item

    测试代码: activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an ...

  4. 【easuyi】---easyui中的验证validatebox自定义

    这里比较简单的使用就不再多说,主要说一下自定义的validatebox. 1.验证密码是否相等,这个直接参考给定的列子就行,这里主要学习这种灵活使用的方式和方法. <input id=" ...

  5. .Net并行编程

    1.什么是线程?线程和进程的区别是什么? 线程是程序执行的最小单元. 区别: 进程是操作系统进行资源处理和分配的最小单位,而一个进程可以包含多个线程,并共享进程的资源. 2.什么是多线程?为什么设计多 ...

  6. linux下的声卡驱动架构

    1.linux下的声卡驱动架构主要分为OSS架构和ALSA架构. 2.OSS架构 OSS全称是Open Sound System,叫做开放式音频系统,这种早期的音频系统这种基于文件系统的访问方式,这意 ...

  7. MvvmCross for WPF 支持子窗体显示、关闭、传参

    最近在做 PCL(Portable Class Library)平台的项目,所以发一下自己遇到的问题 MvvmCross 是 PCL 平台的一个 MVVM 框架 地址:https://github.c ...

  8. Oracle DBLINK 抽数以及DDL、DML操作

    DB :  11.2.0.3.0 原库实例orcl:SQL> select instance_name from v$instance; INSTANCE_NAME--------------- ...

  9. 九度oj 1349 数字在排序数组中出现的次数

    原题链接:http://ac.jobdu.com/problem.php?pid=1349 二分.. #include<algorithm> #include<iostream> ...

  10. C#中的委托与事件

    1,委托? 通俗来讲,就是一个能存放符合某种格式(签名)的方法的指针 的容器  (可以将方法作为一个参数来传递到另一个方法内执行) 定义委托:delegate string DelegateSayHi ...