当前位置:网站首页>968 edit distance
968 edit distance
2022-07-06 21:01:00 【-Lin Zeyu】
The title is as follows
Here are two words for you word1 and word2, Please return to word1 convert to word2 The minimum number of operands used .
You can do the following three operations on a word :
Insert a character
Delete a character
Replace a character
Example 1:
Input :word1 = “horse”, word2 = “ros”
Output :3
explain :
horse -> rorse ( take ‘h’ Replace with ‘r’)
rorse -> rose ( Delete ‘r’)
rose -> ros ( Delete ‘e’)
Example 2:
Input :word1 = “intention”, word2 = “execution”
Output :5
explain :
intention -> inention ( Delete ‘t’)
inention -> enention ( take ‘i’ Replace with ‘e’)
enention -> exention ( take ‘n’ Replace with ‘x’)
exention -> exection ( take ‘n’ Replace with ‘c’)
exection -> execution ( Insert ‘u’)
Tips :
0 <= word1.length, word2.length <= 500
word1 and word2 It's made up of lowercase letters
Their thinking
Dynamic programming
We can do three operations on any word :
Insert a character ;
Delete a character ;
Replace a character .
The title is given two words , Set to A and B, In this way, we can six operation methods .
But we can see , If we have words A And the words B:
Pairs of words A Delete a character and pair words B Inserting a character is equivalent . For example, when the word A by doge, word B by dog when , We can delete words A Last character of e, Get the same dog, You can also use words B Add a character at the end e, Get the same doge;
Empathy , Pairs of words B Delete a character and pair words A Inserting a character is also equivalent ;
Pairs of words A Replace a character and pair words B Replacing a character is equivalent . For example, when the word A by bat, word B by cat when , We modify words A First letter of b -> c, And modify words B First letter of c -> b It is equivalent. .
Since then , There are actually only three different operations :
In the word A Insert a character in ;
In the word B Insert a character in ;
Modify the word A A character of .
Since then , We can turn the original problem into a smaller subproblem . We use it A = horse,B = ros As an example , Let's take a look at how to transform this problem into several smaller sub problems .
In the word A Insert a character in : If we knew horse To ro The editing distance of is a, So clearly horse To ros Your editing distance will not exceed a + 1. This is because we can a After this operation, it will horse and ro Become the same string , Only additional 1 operations , In the word A Add characters at the end of s, You can be in a + 1 After this operation, it will horse and ro Become the same string ;
In the word B Insert a character in : If we knew hors To ros The editing distance of is b, So clearly horse To ros Your editing distance will not exceed b + 1, The reason as above ;
Modify the word A A character of : If we knew hors To ro The editing distance of is c, So clearly horse To ros Your editing distance will not exceed c + 1, The reason as above .
So from horse become ros The editing distance should be min(a + 1, b + 1, c + 1).
Be careful : Why are we always talking about words A and B Insert or modify characters at the end of , Can it be operated in other places ? The answer is yes , But we know that , The sequence of operations does not affect the final result . For example, for words cat, We hope that c and a Add characters between d And put the character t Change to character b, Then the two operations are in whatever order , Will get the final result cdab.
You may think horse To ro This problem is also difficult to solve . But it doesn't matter , We can continue to use the above method to split this problem , For all sub problems of this problem , We can also continue to split , until :
character string A It's empty , If you follow The switch to ro, Obviously, the edit distance is a string B The length of , Here is 2;
character string B It's empty , If you follow horse The switch to , Obviously, the edit distance is a string A The length of , Here is 5.
therefore , We can use dynamic programming to solve this problem . We use it D[i][j] Express A Before i Letters and B Before j The edit distance between letters .
As mentioned above , When we get D[i][j-1],D[i-1][j] and D[i-1][j-1] After that, we can calculate D[i][j].
D[i][j-1] by A Before i Characters and B Before j - 1 Sub problem of character editing distance . That is for B Of the j Characters , We are A The same character is added at the end of the , that D[i][j] The minimum can be D[i][j-1] + 1;
D[i-1][j] by A Before i - 1 Characters and B Before j Sub problem of character editing distance . That is for A Of the i Characters , We are B The same character is added at the end of the , that D[i][j] The minimum can be D[i-1][j] + 1;
D[i-1][j-1] by A front i - 1 Characters and B Before j - 1 Sub problem of character editing distance . That is for B Of the j Characters , We modify A Of the i Two characters make them the same , that D[i][j] The minimum can be D[i-1][j-1] + 1. Specially , If A Of the i Characters and B Of the j The two characters are the same , Then we don't actually need to modify . under these circumstances ,D[i][j] The minimum can be D[i-1][j-1].
Then we can write the following state transition equation :
So the results of each step will be based on the results of the previous step , It is shown as follows :
For boundary cases , The edit distance between an empty string and a non empty string is D[i][0] = i and D[0][j] = j,D[i][0] It's equivalent to word1 perform i Delete operations ,D[0][j] It's equivalent to word1 perform j Insert operations .
To sum up, we get the whole process of the algorithm .
Solution code
class Solution
{
public:
int minDistance(string word1, string word2)
{
int n = word1.length();
int m = word2.length();
// There is a string that is empty
if (n * m == 0) return n + m;
// DP Array
vector<vector<int>> D(n + 1, vector<int>(m + 1));
// Boundary state initialization
for (int i = 0; i < n + 1; i++)
{
D[i][0] = i;
}
for (int j = 0; j < m + 1; j++)
{
D[0][j] = j;
}
// Calculate all DP value
for (int i = 1; i < n + 1; i++)
{
for (int j = 1; j < m + 1; j++)
{
int left = D[i - 1][j] + 1;
int down = D[i][j - 1] + 1;
int left_down = D[i - 1][j - 1];
if (word1[i - 1] != word2[j - 1]) left_down += 1;
D[i][j] = min(left, min(down, left_down));
}
}
return D[n][m];
}
};
边栏推荐
- Reference frame generation based on deep learning
- Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
- New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
- R language visualizes the relationship between more than two classification (category) variables, uses mosaic function in VCD package to create mosaic plots, and visualizes the relationship between tw
- use. Net drives the OLED display of Jetson nano
- 监控界的最强王者,没有之一!
- The mail command is used in combination with the pipeline command statement
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
- 面试官:Redis中有序集合的内部实现方式是什么?
- 【微信小程序】運行機制和更新機制
猜你喜欢

基于深度学习的参考帧生成

1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效

Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
![[MySQL] trigger](/img/b5/6df17eb254bbdb0aba422d08f13046.png)
[MySQL] trigger

Logic is a good thing

Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
![[weekly pit] output triangle](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[weekly pit] output triangle

Swagger UI tutorial API document artifact

请问sql group by 语句问题

防火墙基础之外网服务器区部署和双机热备
随机推荐
1_ Introduction to go language
C language operators
It's almost the new year, and my heart is lazy
7、数据权限注解
防火墙基础之外网服务器区部署和双机热备
Huawei device command
Detailed explanation of knowledge map construction process steps
What key progress has been made in deep learning in 2021?
PHP saves session data to MySQL database
[asp.net core] set the format of Web API response data -- formatfilter feature
Minimum cut edge set of undirected graph
Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
I've seen many tutorials, but I still can't write a program well. How can I break it?
Summary of different configurations of PHP Xdebug 3 and xdebug2
[MySQL] basic use of cursor
c#使用oracle存储过程获取结果集实例
[200 opencv routines] 220 Mosaic the image
Yyds dry goods count re comb this of arrow function
监控界的最强王者,没有之一!
Web开发小妙招:巧用ThreadLocal规避层层传值