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.mzph.cn/diannao/52220.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

10 Java数据结构:包装类、数组(Array工具类)、ArrayList

文章目录 前言一、包装类1、Integer&#xff08;1&#xff09;基本用法&#xff08;2&#xff09;JDK5前的包装类用法&#xff08;了解即可&#xff0c;能更好帮助我们理解下面的自动装箱和自动拆箱机制&#xff09;&#xff08;3&#xff09;自动装箱与自动拆箱机制 --- 导致&…

【学习笔记】Day 21

一、进度概述 1、机器学习常识19-22&#xff0c;以及相关代码复现 二、详情 19、矩阵分解 矩阵分解是一个纯数学问题&#xff0c;但当给矩阵赋予现实意义后&#xff0c;矩阵分解就成为了使用数学应对机器学习问题的一类典型而巧妙的方法。 在线性回归分析中&#xff…

esp32c3 luaos

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、介绍二、相关介绍2.1helloworld——2.2任务框架2.3消息传递 与消息订阅2.4uart2.5二进制数据/c结构体的打包与解析2.6 zbuffer库2.8 uart 485 数据解析2.9 …

深入探讨 ElementUI 动态渲染 el-table

在前端开发中&#xff0c;表格是不可或缺的一部分。无论是数据展示、数据录入&#xff0c;还是数据分析&#xff0c;表格都扮演着重要的角色。而在 Vue.js 生态系统中&#xff0c;ElementUI 提供了一个强大且灵活的表格组件——el-table。本文将带你深入了解如何使用 ElementUI…

缓存实现方式

缓存是一个常见的话题&#xff0c;因为它对于提高应用程序性能至关重要。缓存是一种存储数据的临时地方&#xff0c;以便快速访问数据&#xff0c;减少对原始数据源&#xff08;如数据库或文件系统&#xff09;的访问次数&#xff0c;从而提高应用程序的响应速度和吞吐量。 Jav…

【运维】Linux如何解压.rar文件

在Linux系统中解压.rar文件,你可以使用unrar或rar工具。如果系统中还没有安装它们,可以先通过包管理器进行安装。以下是具体步骤: 1. 安装 unrar 对于基于Debian的发行版(如Ubuntu):sudo apt-get install unrar对于基于Red Hat的发行版(如CentOS、Fedora):sudo yum i…

【Nodejs】六、express框架

目录 一、express 介绍 二、express 使用 2.1 express 下载 2.2 express 使用 三、express 路由 3.1 什么是路由 3.2 路由的使用 3.3 获取请求参数 3.4 获取路由参数 四、express 响应设置 五、express 中间件 5.1 什么是中间件 5.2 中间件的作用 5.3 中间件的类…

如何应对突发技术故障和危机:开发团队的应急策略

开发团队如何应对突发的技术故障和危机&#xff1f; 在数字化时代&#xff0c;软件服务的稳定性对于企业至关重要。然而&#xff0c;即使是大型平台&#xff0c;如网易云音乐&#xff0c;也可能遇到突发的技术故障。网页端出现502 Bad Gateway 报错&#xff0c;且App也无法正常…

如何在VMware ESXI中创建Linux虚拟机并实现异地SSH远程访问

目录 ⛳️推荐 前言 1. 在VMware ESXI中创建Ubuntu虚拟机 2. Ubuntu开启SSH远程服务 3. 安装Cpolar工具 4. 使用SSH客户端远程访问Ubuntu 5. 固定TCP公网地址 ⛳️推荐 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不…

生产环境docker nginx+php8.0镜像

生产环境docker nginxphp8.0镜像 自定义创建php8.0镜像&#xff0c;创建dockerfile FROM php:8.0-fpm# 安装系统依赖 RUN sed -i s|http://deb.debian.org/debian|http://mirrors.aliyun.com/debian|g /etc/apt/sources.list && \apt-get update && apt-get i…

重塑“我店”平台:绿色积分引领的数字消费新纪元

在数字化转型的洪流中&#xff0c;“我店”平台凭借其创新的绿色积分体系异军突起&#xff0c;成为市场中的璀璨新星。本文将深度剖析“我店”的运营模式、市场效应及其如何通过绿色积分机制开创消费新潮流。 一、崛起之路与市场震撼力 自2021年盛夏在上海启航以来&#xff0c…

docker-实战——consul集群

使用docker方式安装consul集群 环境准备 操作系统openEuler 23.09docker环境docker-compose环境本次计划部署的consul集群有3个节点,都是server类型 docker pull registry.cn-hangzhou.aliyuncs.com/bcbx/consul:1.7.8 网络不通的情况下使用上述方式进行镜像拉取 host网络节点…

WPS宏实现Sheet页拆分功能

源表格首列名称中一样的分别创建该名称的Sheet页&#xff0c;首先把首列复制导致Sheet2页&#xff0c;根据去重后的值创建新的Sheet页&#xff0c;把源表格数据复制到新建的Sheet页&#xff0c;遍历删除不需要的留下需要的就完成了。 function JIn521() { //设置工作…

ffmpeg读取时长、读取视频格式

ffmpeg读取时长、读取视频格式 ffmpeg读取时长ffmpeg读取视频格式 ffmpeg读取时长 命令命令介绍具体用法ffmpeg -i查看视频时长ffmpeg -i 视频链接 or 视频路径 2>&1 | grep Duration ffmpeg读取视频格式 命令命令介绍具体用法ffmpeg -i查看视频时长ffmpeg -i 视频链接…

Java CompletableFuture:你真的了解它吗?

文章目录 1 什么是 CompletableFuture&#xff1f;2 如何正确使用 CompletableFuture 对象&#xff1f;3 如何结合回调函数处理异步任务结果&#xff1f;4 如何组合并处理多个 CompletableFuture&#xff1f; 1 什么是 CompletableFuture&#xff1f; CompletableFuture 是 Ja…

<数据集>商品条形码识别数据集<目标检测>

数据集格式&#xff1a;VOCYOLO格式 图片数量&#xff1a;3748张 标注数量(xml文件个数)&#xff1a;3748 标注数量(txt文件个数)&#xff1a;3748 标注类别数&#xff1a;1 标注类别名称&#xff1a;[Barcode] 序号类别名称图片数框数1Barcode37484086 使用标注工具&am…

探索Qotom Q51251OPS迷你电脑:功能与广泛应用

Qotom Q51251 OPS&#xff08;开放可插拔规范&#xff09;迷你电脑是一款设计紧凑且功能强大的设备&#xff0c;旨在满足不同领域的多样化需求。基于英特尔Core i5-12450H Alder Lake H处理器&#xff0c;这款设备不仅具备出色的计算性能&#xff0c;还提供了丰富的连接选项&am…

【MySQL】数据库基础(库的操作)

目录 一、MySQL安装、连接、修改密码操作 二、库的操作 2.1 创建数据库 2.2 字符集和校验规则 2.3 操控数据库 2.4 修改数据库 2.5 删除数据库 2.6 数据库的备份和恢复 2.7 查看连接情况 前情提要&#xff1a; 我的服务器操作系统是Ubuntu20.04&#xff0c;安装的是M…

关于Spring Boot的自动配置

目录 1.EnableAutoConfiguration注解 2.SpringBootConfiguration注解 3.Import注解 4.spring.factories 5.总结 &#xff08;1&#xff09;EnableAutoConfiguration &#xff08;2&#xff09;AutoConfigurationImportSelector &#xff08;3&#xff09; SpringFactoriesLoade…

【好书推荐】值得深读的EMC参考书籍

以下排序不分先后&#xff0c;都是好书&#xff01; 书名&#xff1a;Electromagnetic Compatibility Engineering 作者&#xff1a;Henry W. Ott 这本书的讨论重点放在了经济高效的EMC设计上&#xff0c;并将数学的数量和复杂度保持在最低限度&#xff0c;辅以 250 多个带有…