当前位置:网站首页>11.< tag-动态规划和子序列, 子数组>lt.115. 不同的子序列 + lt. 583. 两个字符串的删除操作 dbc
11.< tag-动态规划和子序列, 子数组>lt.115. 不同的子序列 + lt. 583. 两个字符串的删除操作 dbc
2022-07-28 05:08:00 【菜菜的大数据开发之路】
lt.115. 不同的子序列
[案例需求]

[思路分析]
补充两个讲的很好的题解:
- 这道题目如果不是子序列,而是要求连续序列的,那就可以考虑用KMP。
- 这道题目相对于72. 编辑距离,简单了不少,因为本题相当于只有删除操作,不用考虑替换增加之类的。
但相对于刚讲过的动态规划:392.判断子序列 (opens new window)就有难度了,这道题目双指针法可就做不了了,来看看动规五部曲分析如下:
- 确定dp数组以及下标的含义
dp[i][j]: 以i - 1为结尾的s子序列中出现以j - 1为结尾的t的个数为dp[i][j]
- 确定递推公式
这一类问题, 要分两种情况;
s[i - 1] 与 t[j - 1] 相等s[i - 1]和t[j - 1] 不相等
dp[i][j] = dp[i - 1][j];
- dp数组如何初始化
- 确定遍历顺序
- 举例推导dp数组
[代码实现]
class Solution {
public int numDistinct(String s, String t) {
//1. 确定dp数组及其含义.
//dp[i][j] 为0~i-1字串中出现了0~j-1的字串t的个数
int s_len = s.length();
int t_len = t.length();
int[][] dp = new int[s_len + 1][t_len + 1];
//2.递推公式
//s字串中找到能够组成t字符串的字串(不连续)
//那么0~len -2 也就是 s[i - 1] 是否能够凑出来 t[j - 1], 也就是 j - 1长度的字串就成了切入点;
//如果可以, s[i - 1] 能够凑出来t[j - 1], 那么dp[i][j] = dp[i - 1][j - 1];
//s[i] == t[j], dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; (匹配 + 不匹配)
//s[i] != t[j], dp[i][j] = dp[i - 1][j]; (只能不匹配,)
//3. 初始化
//由递推公式, i, j都是从1开始遍历的, 所以 dp[0][0], dp[i][0]都是需要初始化的.
//dp[0][0] = 1, dp[i][0] = 1;
for(int i = 0; i < s_len + 1; i++){
dp[i][0] = 1;
}
//4. 确定遍历方式. 从左到右
for(int i = 1; i < s_len + 1; i++){
for(int j = 1; j < t_len + 1; j++){
if(s.charAt(i - 1) == t.charAt(j - 1)){
//当前的s[i]和t[j]字符是匹配的,但是我们由选和不选两种情况, 都需要加上才行.
// 选的话s[i]就需要选中, t[j]也相应的匹配上了, 那么就前面的字符匹配就需要写为dp[i - 1][j - 1]
//如果不选的话,s[i]就不会被考虑. 大那是t[j]仍需要s[i]之前的元素去匹配, 就为dp[i - 1][j]
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
}else{
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[s_len][t_len];
}
}
lt.583. 两个字符串的删除操作
[案例需求]

[思路分析]
待补充
[代码实现]
边栏推荐
- Jsonp single sign on permission verification
- After easycvr is connected to the national standard equipment, how to solve the problem that the equipment video cannot be played completely?
- Applet import project
- HDU 3666 the matrix problemdifferential constraint + stack optimization SPFA negative ring
- [high CPU consumption] software_ reporter_ tool.exe
- go-zero单体服务使用泛型简化注册Handler路由
- Using RAC to realize the sending logic of verification code
- RT_ Use of thread message queue
- HDU 3078 network (lca+ sort)
- POJ 1330 Nearest Common Ancestors (lca)
猜你喜欢

How does Alibaba use DDD to split microservices?

FreeRTOS personal notes - task notification

Read the paper -- a CNN RNN framework for clip yield prediction

What should testers know about login security?

【ARXIV2205】EdgeViTs: Competing Light-weight CNNs on Mobile Devices with Vision Transformers

HashSet add

How to send and receive reports through outlook in FastReport VCL?

【ARIXV2204】Neighborhood attention transformer

Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)

Testcafe's positioning, operation of page elements, and verification of execution results
随机推荐
When initializing with pyqt5, super() and_ init _ () problems faced by the coordinated use of functions, as well as the corresponding learning and solutions
Method of converting UI file to py file
Design and development of C language ATM system project
面试了一位38岁程序员,听说要加班就拒绝了
Easycvr Video Square snapshot adding device channel offline reason display
What is the core value of testing?
HDU 1435 stable match
Testcafe's positioning, operation of page elements, and verification of execution results
[learning record] data enhancement 1
Online sql to XML tool
Read the paper -- a CNN RNN framework for clip yield prediction
The research group passed the thesis defense successfully
Duoyu security browser will improve the security mode and make users browse more safely
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
HDU 2874 connections between cities
POJ 2763 housewife wind (tree chain partition + edge weighting point weight)
RT based_ Distributed wireless temperature monitoring system of thread (I)
Improve the core quality of steam education among students
Why is MD5 irreversible, but it may also be decrypted by MD5 free decryption website
Array or object, date operation



