Leetcode#115 Distinct Subsequences
转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的
举个例子,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的更多相关文章
- [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 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]115. Distinct Subsequences 计算不同子序列个数
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 115. Distinct Subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- JavaScript相关知识
JavaScript的语法规则 l JavaScript区分大小写 比如变量a和变量A是不一样的变量,要严格区分大小写 l JavaScript脚本程序须嵌入在HTML文件中 因为javascript ...
- C++ 里 构建动态二维数组
//****动态二维数组 /* int m=3; int **data; int n=2; data=new int*[m]; for(int j=0;j<m;j++) { data[j]=ne ...
- Laravel 5 基础(六)- 数据库迁移(Migrations)
database migrations 是laravel最强大的功能之一.数据库迁移可以理解为数据库的版本控制器. 在 database/migrations 目录中包含两个迁移文件,一个建立用户表, ...
- Java jdk环境变量配置
首先安装jdk,现在已经是jdk 8了,也不知道能不能好好用了...
- EMVTag系列8《IC卡公钥证书》
Ø 9F46 IC卡公钥证书 L: NI -C(有条件):如果支持DDA 发卡行认证过的IC 卡公钥 Ø 9F47 IC卡公钥指数 F: b T: 9F47 L: 1 or 3 C:如 ...
- Oracle Imp and Exp (导入和导出) 数据 工具使用
Oracle 提供两个工具imp.exe 和exp.exe分别用于导入和导出数据.这两个工具位于Oracle_home/bin目录下. 导入数据exp 1 将数据库ATSTestDB完全导出,用户名s ...
- Socket(2)
UDP: UDP协议没有socket之间的虚拟链路,也就是说没有“握手”阶段,只是发送接收. 可以想象成直播,直播时如果中间网络不好,不会事后重新播放之前的,而是直接跳过. 也就是说一方只负责发送,一 ...
- ElasticSearch版本升级备忘录(1.5.2至2.3.1)
discovery机制默认为单播,需配置discovery.zen.ping.unicast.hosts:,如果各节点使用默认端口,则只配IP即可(["172.17.4.47", ...
- Newtonsoft.Json版本冲突时参考解决方案
如果同一项目中不同第三方类库分别使用了不同版本的Newtonsoft.Json的情况下,可以在配置文件中添加以下节点,将0.0.0.0-9.0.0.0此区间的Newtonsoft.Json使用全部强制 ...
- golang:slice陷阱
slice陷阱,slice底层指向某个array,在赋值后容易导致array长期被引用而无法释放