当前位置:网站首页>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. 两个字符串的删除操作
[案例需求]

[思路分析]
待补充
[代码实现]
边栏推荐
- Melt cloud x chat, create a "stress free social" habitat with sound
- Specific differences between typedef and define
- FPGA:使用PWM波控制LED亮度
- Microservice failure mode and building elastic system
- php7.1 连接sqlserver2008r2 如何测试成功
- Data security is gradually implemented, and we must pay close attention to the source of leakage
- 驾驭EVM和XCM的强大功能,SubWallet如何赋能波卡和Moonbeam
- Pipe /createpipe
- Message forwarding mechanism -- save your program from crashing
- Special topic of APP performance design and Optimization - poor implementation affecting performance
猜你喜欢

RT_ Use of thread mailbox

Online sql to XML tool

Real intelligence has been certified by two of the world's top market research institutions and has entered the global camp of excellence

Analyze the emotional elements contained in intelligent sweeping robot

What tools do software testers need to know?

这种动态规划你见过吗——状态机动态规划之股票问题(中)

Automated test tool playwright (quick start)

Easycvr Video Square snapshot adding device channel offline reason display

Flink mind map

Array or object, date operation
随机推荐
How does Alibaba use DDD to split microservices?
Interpreting the source code of cfrunloopref
How to analyze fans' interests?
Specific differences between typedef and define
Anaconda common instructions
Dynamic SQL and paging
YUV to uiimage
Leetcode 15. sum of three numbers
Array or object, date operation
你必需要了解的saas架构设计?
HDU 3078 network (lca+ sort)
php7.1 连接sqlserver2008r2 如何测试成功
POJ 3728 the merchant (online query + double LCA)
从微服务基本概念到核心组件-通过一个实例来讲解和分析
HDU 1435 stable match
Implementation of simple upload function in PHP development
Leetcode 18. sum of four numbers
Pipe /createpipe
Clickhouse pit filling note 2: the join condition does not support non equal judgments such as greater than and less than
Professor dongjunyu made a report on the academic activities of "Tongxin sticks to the study of war and epidemic"



