动态规划—distinct-subsequences
题目:
Given a string S and a string T, count the number of distinct subsequences of T in S.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,"ACE"is a subsequence of"ABCDE"while"AEC"is not).
Here is an example:
S ="rabbbit", T ="rabbit"
Return3.
思路:
1. 初始化一个矩阵number[i][j]用来记录字符串T的前j个字符出现在字符串S的前i个字符的次数,当j=0时,令number[i][j]=1;
2. 当S的第i个字符与T的第j个字符不同时,则说明S的第i个字符对number[i][j]没有影响,即number[i][j]=number[i-1][j];
3. 当S的第i个字符与T的第j个字符不同时,则说明S的第i个字符对number[i][j]有影响,number[i][j]除了要算上原来的number[i-1][j],还要算上新的可能性,即number[i-1][j-1].
例子
0 | r | a | b | b | i | t | |
0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
r | 1 | 1 | 0 | 0 | 0 | 0 | 0 |
a | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
b | 1 | 1 | 1 | 1 | 0 | 0 | 0 |
b | 1 | 1 | 1 | 2 | 1 | 0 | 0 |
b | 1 | 1 | 1 | 3 | 3 | 0 | 0 |
i | 1 | 1 | 1 | 3 | 3 | 3 | 0 |
t | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
代码:
public static int result(String str1, String str2){
int len1 = str1.length(), len2 = str2.length();
int[][] res = new int[len1+1][len2+1];
for(int i=0;i<len1+1;i++){
res[i][0] = 1;
}
for(int i=1;i<len1+1;i++){
for(int j=1;j<len2+1;j++){
if(str1.charAt(i-1)!=str2.charAt(j-1))
res[i][j] = res[i-1][j];
else
res[i][j] = res[i-1][j]+res[i-1][j-1];
} }
return res[len1][len2];
}
动态规划—distinct-subsequences的更多相关文章
- 动态规划——Distinct Subsequences
题目大意:给定字符串S和T,现在从S中任选字符组成T,要求输出方案个数. Example 1:Input: S = "rabbbit", T = "rabbit" ...
- 动态规划-Distinct Subsequences
2020-01-03 13:29:04 问题描述: 问题求解: 经典的动态规划题目,一般来说dp题目是递推关系公式难想,但是实际代码量还是比较少的. 有尝试过dfs来做,但是由于时间复杂度是指数级别的 ...
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- Distinct Subsequences ——动态规划
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode之“动态规划”:Distinct Subsequences
题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...
- Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划
Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Leetcode Distinct Subsequences
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 T in S. A subsequen ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- android 任务栈及启动模式
1.一个应用程序一般都是由多个activity组成的.2.任务栈(task stack)(别名back stack后退栈) 记录存放用户开启的activity的.3.一个应用程序一被开启系统就给他分配 ...
- JavaScript toFixed()、toExponential、toPrecision方法
JavaScript toFixed() 定义和用法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 语法 NumberObject.toFixed(num) 参数 描述 ...
- HTML - form 表单提交
form 表单提交 数据发送 disabled:不发送 display_none:发送 type_hidden:发送 readonly:发送 测试 html: <!DOCTYPE html> ...
- VMware 虚拟机的虚拟磁盘编程知识点扫盲之二
目录 目录 前文列表 VDDK 安装 VDDK VixDiskLib VADP 前文列表 VMware 虚拟机的虚拟磁盘编程知识点扫盲之一 VDDK 摘自官方文档:The Virtual Disk D ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_02 递归_3_练习_使用递归计算阶乘
结束条件是乘到 当前数字等于1
- charles_02_模拟弱网测试
前言 用户使用app的场景是多变的,不一定稳定在WiFi或者4G网络下.大多数用户会在地铁.电梯等弱网情况下使用app,这些弱网情况下app常会出现一些数据丢失.闪退.页面展示不友好等情况.在测试过程 ...
- js数组,运算符
- 子页面中ifram高度自使用
HTML: <iframe id="mainframe" name="mainframe" style="width: 100%; border ...
- 【转载】如何编写ROS的第一个程序hello_world
目录 1.工作空间的创建 2.功能包的创建 3.功能包的源代码编写 4.功能包的编译配置 5.功能包的编译 6.功能包的启动运行 既然ROS已经成功安装好了,大家一定很想亲自动动手编一个通过起手式例程 ...
- [Python3] 011 字符串:给你们看看我的内置方法 第三弹
目录 少废话,上例子 1. encode(encoding='utf-8', errors='strict') 2. expandtabs([tabsize=8]) 借此机会简单地说一说 print( ...