当前位置: 首页 > 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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Python、R用RFM模型、机器学习对在线教育用户行为可视化分析|附数据、代码
  • 【排序算法】八大排序(上)(c语言实现)(附源码)
  • [Linux] 关于执行文件路径的变量:$PATH
  • SpringBoot自定义类加载器
  • linux查看网卡速度和pcie速度
  • 【Python零基础】while循环和用户输入
  • SPI驱动学习一(协议原理)
  • 硬件服务器操作系统的选择:Linux 还是 Windows?
  • 地理科学专业| 中国大学排行榜(2024年)
  • PgSQL HashAgg算法 | 第2期 | 版本12的spill溢出磁盘解秘
  • linux之ELK
  • [Hdp] lc552. 学生出勤记录 II(dp+递推+状态定义+状态转移+向前转移+好题)
  • Clichouse数据导出导入(数据迁移)
  • 重头开始嵌入式第二十三天(进程2)
  • Unity | 性能优化
  • [笔记] php常见简单功能及函数
  • Android Studio:GIT提交项目到远程仓库
  • android 一些 utils
  • Angular6错误 Service: No provider for Renderer2
  • input的行数自动增减
  • Javascript基础之Array数组API
  • Java的Interrupt与线程中断
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • magento 货币换算
  • Median of Two Sorted Arrays
  • SQLServer插入数据
  • underscore源码剖析之整体架构
  • v-if和v-for连用出现的问题
  • yii2中session跨域名的问题
  • 编写符合Python风格的对象
  • 测试如何在敏捷团队中工作?
  • 复习Javascript专题(四):js中的深浅拷贝
  • 诡异!React stopPropagation失灵
  • 使用agvtool更改app version/build
  • 数据可视化之 Sankey 桑基图的实现
  • 微信开源mars源码分析1—上层samples分析
  • ​3ds Max插件CG MAGIC图形板块为您提升线条效率!
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • #pragma data_seg 共享数据区(转)
  • #Ubuntu(修改root信息)
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (C++17) std算法之执行策略 execution
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (Mac上)使用Python进行matplotlib 画图时,中文显示不出来
  • (非本人原创)史记·柴静列传(r4笔记第65天)
  • (附源码)spring boot火车票售卖系统 毕业设计 211004
  • (附源码)计算机毕业设计SSM基于健身房管理系统
  • (七)Knockout 创建自定义绑定
  • (十)DDRC架构组成、效率Efficiency及功能实现
  • (微服务实战)预付卡平台支付交易系统卡充值业务流程设计
  • (五)c52学习之旅-静态数码管
  • (一)Kafka 安全之使用 SASL 进行身份验证 —— JAAS 配置、SASL 配置
  • (一)UDP基本编程步骤
  • (转)大型网站的系统架构
  • (转)真正的中国天气api接口xml,json(求加精) ...