编辑距离 dp_使用动态编程(DP)编辑距离

编辑距离 dp

Problem: You are given two strings s1 and s2 of length M and N respectively. You can perform following operations on the string.

问题:给您两个长度分别为MN的字符串s1s2 。 您可以对字符串执行以下操作。

  1. Insert a character at any position

    在任意位置插入字符

  2. Delete a character at any position

    删除任意位置的字符

  3. Replace a character with any character at any position

    用任意位置的任何字符替换字符

Find minimum number of ways to convert s1 into s2 using above operation only.

仅使用上述操作找出将s1转换为s2的最小方法。

Constraints:

限制条件:

1 <= N <= 2000
1 <= M <= 2000

Example:

例:

    Sample Input 1:
abcde
bcdae
Sample Output 1:
2
Sample Input 2:
dacef
cdafe
Sample Output 2:
3

Explanation of the problem:

问题说明:

In the first sample provided above, we can delete a from s1 and insert it before e in s1. So, there are two steps.

在上面提供的第一个示例中,我们可以从s1中删除a并将其插入s1中的 e之前。 因此,有两个步骤。

Solution:

解:

Before proceeding to the solution we can see that the recursive solution will be hard to implement so we will proceed to the dynamic programming approach. In this approach, the jth cell of ith row represents the minimum number of changes needed to change s1(ith index to end) and s2(jth index to end). Now if an ith character of s1 is equal to a jth character of s2 then we don’t have to take care of that character as it is already same so pick up the right-bottom diagonal value. If characters are not equal then we can delete the ith character of the s1 or jth character of s2, replace the ith character of s1 with the jth character of s2 or vice versa and move on to the right bottom corner. In this case, we also have to add 1 as deletion, replacement is considered as a step.

在继续解决方案之前,我们可以看到递归解决方案将很难实现,因此我们将继续使用动态编程方法。 在这种方法中, i行的 j 单元表示更改s1( i 索引到结束)和s2( j 索引到结束)所需的最小更改次数。 现在,如果s1的 i 字符等于s2的 j 字符,那么我们就不必照顾这个字符了,因为它已经是相同的了,所以选择右下角的对角线值。 如果字符不相等,那么我们可以删除s1的 i 字符或s2的 j 字符,将s1的 i 字符替换为s2的 j 字符,反之亦然,然后移至右下角。 在这种情况下,我们还必须添加1作为删除,替换被视为一个步骤。

Algorithm:

算法:

  1. Create dp matrix in which jth cell of ith row represents the minimum number of ways to change the string s1 (from ith index to last) and string s2 (jth index to last).

    创建dp矩阵,其中 i行的 j 单元格表示更改字符串s1 (从 i 索引到最后一个)和字符串s2 ( j 索引到最后一个)的最小方式。

  2. One extra row and col are taken to build to include blank string also.

    还需要多一行和col来构建以包括空白字符串。

  3. If s1 is blank and s2 is full we have to initialize this condition so initializing this condition doing the same thing for vice versa case.

    如果s1为空白,而s2为满,则必须初始化此条件,因此对这种情况进行初始化的情况与之相反。

  4. Start filling dp matrix from the Nth row and Mth column (right to left and down to up).

    开始从 N行和M填充DP矩阵 (从右到左,下到上)。

  5. Find the answer of each row by using dp relations.

    通过使用dp关系找到每一行的答案。

  6. If ith character of s1 is same as a jth character of s2 then store the right-bottom value.

    如果I S1字符是相同的 j S2的字符然后存储右下角值。

  7. Otherwise, take the minimum of downward adjacent, rightward adjacent, bottom-right adjacent value and add 1 to it and store the answer.

    否则,取下相邻,右相邻,右下相邻值中的最小值,并将其加1并存储答案。

  8. Store the answer in a jth cell of an ith row.

    将答案存储在 i行的 j 单元格中。

The time complexity of the above code is O (N * M).

上面代码的时间复杂度是O(N * M)。

使用动态编程(DP)编辑距离的C ++代码 (C++ Code for Edit Distance using Dynamic Programming (DP))

#include <iostream>
#include <math.h>
using namespace std;
// function for finding edit distance
int editDistance(string s1, string s2){
// dp matrix for storing the values
int dp[s1.length() + 1][s2.length() + 1] = {0};
int counter = 0;
// loops are used to store some values necessary for dp relations as 
// we can initialize all values to 0 in this case
for(int row = s1.length();row>=0;row--){
dp[row][s2.length()] = counter;
counter++;
}
counter = 0;
for(int col = s2.length();col>=0;col--){
dp[s1.length()][col] = counter;
counter++;
}
// filling the dp matrix 
for(int row = s1.length()-1;row>=0;row--){
for(int col = s2.length() - 1;col>=0;col--){
// if rowth character of s1 is same as colth character of s2 
// then store diagonally right-bottom shifted value
if(s1[row] == s2[col]){
dp[row][col] = dp[row+1][col+1];
}
// otherwise, take minimum of adjacent downward, adjacent rightward, 
// diagonally rigth-bottom value and add 1 to it and store that value
else{
dp[row][col] = min(dp[row+1][col], min(dp[row][col+1], dp[row+1][col+1])) + 1;
}
}
}
return dp[0][0];
}
// driver function to test the code
int main() {
string s1,s2;
cin >> s1 >> s2;
cout << s1 << "\n" << s2 << endl;
cout<< editDistance(s1, s2);
return 0;
}

Output

输出量

abcde
bcdae
abcde
bcdae
2   

翻译自: https://www.includehelp.com/algorithms/edit-distance-using-dynamic-programming.aspx

编辑距离 dp

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541915.shtml

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

相关文章

tomcat +apache 配置集群

2019独角兽企业重金招聘Python工程师标准>>> APACHE2.2.25TOMCAT6.0.37配置负载均衡 目标: 使用 apache 和 tomcat 配置一个可以应用的 web 网站&#xff0c;要达到以下要求&#xff1a; 1. Apache 做为 HttpServer &#xff0c;后面连接多个 tomcat 应用实例&…

java双缓存机制_详解JVM类加载机制及类缓存问题的处理方法

前言大家应该都知道&#xff0c;当一个Java项目启动的时候&#xff0c;JVM会找到main方法&#xff0c;根据对象之间的调用来对class文件和所引用的jar包中的class文件进行加载(其步骤分为加载、验证、准备、解析、初始化、使用和卸载)&#xff0c;方法区中开辟内存来存储类的运…

oracle中dbms_并发和由于DBMS中的并发导致的问题

oracle中dbms并发 (Concurrency) The ability of a database system which handles simultaneously or a number of transactions by interleaving parts of the actions or the overlapping this is called concurrency of the system. 数据库系统通过交织部分操作或重叠操作来…

什么是mvc?

什么是MVCMVC 是一种设计模式&#xff0c;它将应用划分为3 个部分&#xff1a;数据&#xff08;模型&#xff09;、展现层&#xff08;视图&#xff09;和用户交互层&#xff08;控制器&#xff09;。换句话说&#xff0c;一个事件的发生是这样的过程&#xff1a;1&#xff0e;…

mysql的安装和基本命令_MySQL安装以及简单命令用法

MYSQL:关系型数据库存储引擎:负责将逻辑层的概念转化为物理层机制&#xff0c;在物理层完成物理机制。支持事务&#xff1a;transaction必须满足的条件&#xff1a;ACID(一致性,持久性,原子性,隔离性)锁&#xff1a;并发访问随机访问&#xff1a;数据在磁盘上是随机存储的安装&…

将数组转换为JavaScript中的对象

Lets say you have the following array, 假设您有以下数组&#xff0c; const nums [1, 2, 3, 4, 5];console.log(nums);Output 输出量 (5) [1, 2, 3, 4, 5]We know that nums is an array and we can see in the output that we get an array. Lets convert it into an ob…

docker集群运行在calico网络上

2019独角兽企业重金招聘Python工程师标准>>> ##网络及版本信息 docker1 centos7 192.168.75.200 docker2 centos7 192.168.75.201 物理网络 192.168.75.1/24 Docker version 1.10.3, build 3999ccb-unsupported &#xff0c;安装过程略 # calicoctl version Version…

python批量雷达图_python批量制作雷达图

老板要画雷达图&#xff0c;但是数据好多组怎么办&#xff1f;不能一个一个点excel去画吧&#xff0c;那么可以利用python进行批量制作&#xff0c;得到样式如下&#xff1a;首先制作一个演示的excel&#xff0c;评分为excel随机数生成&#xff1a;1 INT((RAND()4)*10)/10加入标…

JavaScript中带有示例的Math.log()方法

JavaScript | Math.log()方法 (JavaScript | Math.log() Method) Math.log() is a function in math library of JavaScript that is used to return the value of natural Log i.e. (base e) of the given number. It is also known as ln(x) in mathematical terms. Math.log…

SUI踩坑记录

SUI踩坑记录 最近做了个项目选型了SUI和vue做单页应用。下面记录一下踩坑经历SUI 介绍 sui文档&#xff1a;http://m.sui.taobao.org/SUI Mobile 是一套基于 Framework7 开发的UI库。它非常轻量、精美&#xff0c;只需要引入我们的CDN文件就可以使用&#xff0c;并且能兼容到 i…

java 写入xml文件_java读写xml文件

要读的xml文件李华姓名>14年龄>学生>张三姓名>16年龄>学生>学生花名册>package xml;import java.io.FileOutputStream;import java.io.OutputStreamWriter;import java.io.Writer;import java.util.Iterator;import java.util.Vector;import javax.xml.pa…

JavaScript中带有示例的Math.max()方法

JavaScript | Math.max()方法 (JavaScript | Math.max() Method) Math.max() is a function in math library of JavaScript that is used to return the greatest value of all the passed values to the method. Math.max()是JavaScript数学库中的函数&#xff0c;用于将所有…

java 修饰符默认_Java和C#默认访问修饰符

C#中&#xff1a;针对下面几种类型内部成员的访问修饰符&#xff1a;enum的默认访问修饰符&#xff1a;public。class的默认为private。interface默认为public。struct默认为private。其中&#xff1a;public可以被任意存取&#xff1b;protected只可以被本类和其继承子类存取&…

JavaScript中带有示例的Math.abs()方法

JavaScript | Math.abs()方法 (JavaScript | Math.abs() Method) Math operations in JavaScript are handled using functions of math library in JavaScript. In this tutorial on Math.abs() method, we will learn about the abs() method and its working with examples.…

人脸识别python face_recognize_python2.7使用face_recognition做人脸识别

偶然看到一篇文章&#xff0c;说是可以实时人脸识别&#xff0c;很有兴趣就自己按照文章开始动手人脸识别&#xff0c;但是实现过程中遇到了几个问题这里做个总结&#xff0c;希望可以帮助到大家安装face_recognition这个之前需要先安装编译dlib&#xff0c;如果没有安装dlib&a…

c# reverse_清单 .Reverse()方法,以C#为例

c# reverseC&#xff03;List <T> .Reverse()方法 (C# List<T>.Reverse() Method) List<T>.Reverse() method is used to reverse the all list elements. List <T> .Reverse()方法用于反转所有列表元素。 Syntax: 句法&#xff1a; void List<T&…

cpuinfo详解

cat /proc/cpuinfo processor: 23&#xff1a;超线程技术的虚拟逻辑核第24个 ###一般看最后一个0...23 表示24线程 vendor_id: GenuineIntel&#xff1a;CPU制造商cpu family: 6&#xff1a;CPU产品系列代号model: 44&#xff1a;CPU属于其系列中的哪一代号model name: Intel…

jvm延迟偏向_用于偏向硬币翻转模拟的Python程序

jvm延迟偏向Here, we will be simulating the occurrence coin face i.e. H - HEAD, T - TAIL. Simply we are going to use an inbuilt library called as random to call a random value from given set and thereby we can stimulate the occurrence value by storing the o…

java项目没有bin_WebAPI项目似乎没有将转换后的web.config发布到bin文件夹?

我很擅长.NET配置转换 . 我现在将它们放在用于数据使用的类库和WPF应用程序上 .但是&#xff0c;当我尝试使用ASP.NET WebAPI项目进行设置时&#xff0c;似乎发生了一些奇怪的事情 .配置文件永远不会显示在我的bin目录中&#xff0c;因此web.config始终显示为预先形成的配置文件…