当前位置: 首页 > news >正文

day45-dynamic programming-part12-8.16

tasks for today:

1. 115.不同的子序列

2. 583.两个字符串选的删除操作

3. 72.编辑距离

4. 总结编辑两个序列关系的问题

-------------------------------------------------------------------

1. 115.不同的子序列

In this practice, it is necessary to compare with the practice 392, the essense of practice 392 is finding the length of longest common string, while in this practice, the dp reocrd the times that t[0:j] appears in s[:i];

the essence is the recursive equation and the initialization. 

recursive equation: discerned by if s[i-1] == t[j-1], when true, the dp[i][j] actually consist of two parts, the dp[i-1][j-1] is easy to understand, the dp[i-1][j] is a thinker, this actually include all the nums of condition where t[:j] appears in s section which is shorter thans[s:i].

The initialization's key point is the dp[i][0] = 1 and dp[0][j] = 0, void string t="" is counted as a child string, that is why the dp[i][0] is initialized as 1.

class Solution:def numDistinct(self, s: str, t: str) -> int:if len(s) < len(t): return 0if len(s) >= len(t):if len(t) == 1 and t[0] not in s: return 0dp = [[0] * (len(t)+1) for _ in range(len(s)+1)]for i in range(len(s)):dp[i][0] = 1for i in range(1, len(s)+1):for j in range(1, len(t)+1):if s[i-1] == t[j-1]:dp[i][j] = dp[i-1][j-1] + dp[i-1][j]else:dp[i][j] = dp[i-1][j]return dp[-1][-1]

2. 583.两个字符串选的删除操作

In this practice, the key difference are also lied in the recursive equation and the initialization.

It is intuitive to define the dp[i][j] as the min actions to make word1[:i] and word2[:j] equal.

the essence of the recursive equation is when the word1[i-1] != word2[j-1], the state of dp[i][j] can be derived from the dp[i-1][j] or dp[i][j-1] or dp[i-1][j-1]

class Solution:def minDistance(self, word1: str, word2: str) -> int:dp = [[0] * (len(word2)+1) for _ in range(len(word1)+1)]for i in range(1, len(word1)+1):dp[i][0] = ifor j in range(1, len(word2)+1):dp[0][j] = jfor i in range(1, len(word1)+1):for j in range(1, len(word2)+1):if word1[i-1] == word2[j-1]:dp[i][j] = dp[i-1][j-1]else:dp[i][j] = min(dp[i-1][j-1]+2, dp[i-1][j]+1, dp[i][j-1]+1)return dp[-1][-1]

3. 72.编辑距离

This practice is an extension of practice 583, which allows for more operations other than deleting.

The essense of the recursive equation also lies in the operation of condition "word1[i-1] != word2[j-1]". Be noted that the deleting of word1 is equal to addition on word2, since this practice count the least number of operation, instead of options of operation, so it is ok only choose one apprah.

class Solution:def minDistance(self, word1: str, word2: str) -> int:dp = [[0] * (len(word2)+1) for _ in range(len(word1)+1)]for i in range(1, len(word1)+1):dp[i][0] = ifor j in range(1, len(word2)+1):dp[0][j] = jfor i in range(1, len(word1)+1):for j in range(1, len(word2)+1):if word1[i-1] == word2[j-1]:dp[i][j] = dp[i-1][j-1]else:dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1return dp[-1][-1]

4. a summary of practice 392/115/583/72

these 4 practices are related to the relationship / edition of two strings or sequence, 

(1) from the configuration:

practice 392/115 discuss the relationship, which 392 concentrated on true/false of containing, while practice 115 focuses on the times of containing.

practice 583/72 dicuss the edition of two lists for making them to achieve a specific requirement such as being same, which 583 concentrates on only using deleting, while 72 allows for more operations such as replacement or addition.

(2) from the code-dp:

392's dp records the max length of common string, while 115 records the times of containing;

583's dp records the delete times, while the 72 records the times of operaaation

(3) from the code-initialization:

all of them need to initialize the dp[i][0] and dp[0][j]

392 update them as 1

115 of update dp[i][0] as 1

583/72 update dp[i][0] as i, while dp[0][j] as j

http://www.lryc.cn/news/428019.html

相关文章:

  • C# String的方法
  • Oracle RAC vs Clusterware vs ASM
  • “华为杯”第十五届中国研究生数学建模竞赛-F题:机场新增卫星厅对中转旅客影响的研究
  • 正点原子linux开发板 qt程序交叉编译执行
  • 聚星文社和虹猫哪个好
  • 三十八、【人工智能】【机器学习】【监督贝叶斯网络(Bayesian Networks)学习】- 算法模型
  • [书生大模型实战营][L0][Task1] Linux 远程连接 InternStudio
  • 【vue教程】六. Vue 的状态管理
  • 无人机电子调速器详解!!!
  • Clichouse数据导出导入(数据迁移)
  • Java基础——IService.class 中查询数据方法list() 源码剖析及使用
  • MySQL库表的基本操作
  • 基于ResNeSt50神经网络模型的蘑菇分类设计与实现,使用注意力机制,分别对应8种蘑菇进行训练预测
  • [论文翻译]使用 BERT 检测安卓恶意软件
  • LabVIEW滚动轴承故障诊断系统
  • 【论文分享】通过社交媒体图片和计算机视觉分析城市绿道的使用情况
  • MySQL 在 Windows 和 Ubuntu 上的安装与远程连接配置简介
  • 博达网站群管理平台 v6.0使用相关问题解决
  • C++—>STL中vector使用篇
  • pyinstaller打包vnpy项目
  • kafka 消费组 分区分配策略
  • AQS原理解析
  • 『 Linux 』利用UDP套接字实现简单群聊
  • 【数据结构与算法 | 图篇】最小生成树之Kruskal(克鲁斯卡尔)算法
  • 了解常用的代码检查工具
  • BUUCTF PWN wp--warmup_csaw_2016
  • dockerfile搭建部署LNMP
  • Rust : 数据分析利器polars用法
  • Qt第一课
  • 论“graphics.h”库,easyx