Edit Distance
题目描述
The edit distance between two strings is the minimum number of operations required to transform one string into the other.
The allowed operations are:
Add one character to the string.
Remove one character from the string.
Replace one character in the string.
For example, the edit distance between LOVE and MOVIE is 2, because you can first replace L with M, and then add I.
Your task is to calculate the edit distance between two strings.
输入
The first input line has a string that contains n characters between A–Z.
The second input line has a string that contains m characters between A–Z.
Constraints
1 ≤ n,m ≤ 5000
输出
Print one integer: the edit distance between the strings.
样例输入
LOVE MOVIE
样例输出
2
思路分析
本题样例求解:
j=0 j=1 j=2 j=3 j=4 j=5 M O V I E i=0 0 1 2 3 4 5 i=1 L 1 1 2 3 4 5 i=2 O 2 2 1 2 3 4 i=3 V 3 3 2 1 2 3 i=4 E 4 4 3 2 2 2 递归
MOVIE与LOVE最后的字符均为‘E’,可以无痛转化成MOVI与LOV之间的编辑距离。
MOVI与LOV最后的字符不相等,可以考虑对MOVI进行操作,或将其最后一位删除,或将其最后一位改为‘V’,或在其后面加‘V’。
……
如果一个字符串为空而另一个字符串非空,那么编辑距离就为非空字符串的长度。
递归会大量重复计算导致超时。
动态规划
dp[i][j]存储 第一个字符串的前i个字符 与 第二个字符串的前j个字符 的 最短操作距离。
注意初始化,提前处理任一字符串为空的情况。
当a[i]==b[j],dp[i+1][j+1]=dp[i][j]。
当a[i]!=b[j],此时需要对字符串进行增删改三种操作,该操作编辑距离为1,此时dp[i+1][j+1]=min(dp[i][j],dp[i+1][j],dp[i][j+1])+1。
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
string a,b;
int m,n;
int main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>a>>b;m=a.size();n=b.size();vector<vector<int>>dp(m+1,vector<int>(n+1,0));for(int i=0;i<=n;i++){dp[0][i]=i;}for(int j=0;j<=m;j++){dp[j][0]=j;}for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){if(a[i-1]==b[j-1]){dp[i][j]=dp[i-1][j-1];}else{int c[3]={dp[i][j-1],dp[i-1][j],dp[i-1][j-1]};sort(c,c+3);dp[i][j]=c[0]+1;}}}cout<<dp[m][n];return 0;
}