编辑距离 dp
Problem: You are given two strings s1 and s2 of length M and N respectively. You can perform following operations on the string.
问题:给您两个长度分别为M和N的字符串s1和s2 。 您可以对字符串执行以下操作。
Insert a character at any position
在任意位置插入字符
Delete a character at any position
删除任意位置的字符
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:
算法:
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 个索引到最后一个)的最小方式。
One extra row and col are taken to build to include blank string also.
还需要多一行和col来构建以包括空白字符串。
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为满,则必须初始化此条件,因此对这种情况进行初始化的情况与之相反。
Start filling dp matrix from the Nth row and Mth column (right to left and down to up).
开始从第 N行和M填充DP矩阵列 (从右到左,下到上)。
Find the answer of each row by using dp relations.
通过使用dp关系找到每一行的答案。
If ith character of s1 is same as a jth character of s2 then store the right-bottom value.
如果I S1的个字符是相同的第 j 个 S2的字符然后存储右下角值。
Otherwise, take the minimum of downward adjacent, rightward adjacent, bottom-right adjacent value and add 1 to it and store the answer.
否则,取下相邻,右相邻,右下相邻值中的最小值,并将其加1并存储答案。
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