# 编辑距离
给你两个单词 word1
和 word2
,请你计算出将 word1
转换成 word2
所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
示例 1:
输入:word1 = "horse", word2 = "ros"
输出:3
解释:horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')
示例 2:
输入:word1 = "intention", word2 = "execution"
输出:5
解释:intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')
提示:
0 <= word1.length, word2.length <= 500
word1
和 word2
由小写英文字母组成
以下错误的选项是?
## aop
### before
```c
#include
using namespace std;
```
### after
```c
int main()
{
Solution sol;
string word1 = "horse";
string word2 = "ros";
int res;
res = sol.minDistance(word1, word2);
cout << res;
return 0;
}
```
## 答案
```c
class Solution
{
public:
int minDistance(string word1, string word2)
{
int len1 = word1.size();
int len2 = word2.size();
int **dp = new int *[len1 + 1];
for (int i = 0; i < len1 + 1; i++)
dp[i] = new int[len2 + 1];
for (int i = 0; i < len1 + 1; i++)
dp[i - 1][0] = i;
for (int i = 1; i < len2 + 1; i++)
dp[0][i - 1] = i;
for (int i = 1; i < len1 + 1; i++)
{
for (int j = 1; j < len2 + 1; j++)
{
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], min(dp[i - 1][j], dp[i][j - 1])) + 1);
}
}
return dp[len1][len2];
}
};
```
## 选项
### A
```c
class Solution
{
public:
int minDistance(string word1, string word2)
{
int m = word1.size(), n = word2.size();
if (m == 0)
return n;
if (n == 0)
return m;
int dp[m][n];
bool w1 = false, w2 = false;
if (word1[0] == word2[0])
{
w1 = true;
w2 = true;
dp[0][0] = 0;
}
else
dp[0][0] = 1;
for (int i = 1; i < m; i++)
{
if (!w1 && word1[i] == word2[0])
{
w1 = true;
dp[i][0] = dp[i - 1][0];
}
else
dp[i][0] = dp[i - 1][0] + 1;
}
for (int j = 1; j < n; j++)
{
if (!w2 && word1[0] == word2[j])
{
w2 = true;
dp[0][j] = dp[0][j - 1];
}
else
dp[0][j] = dp[0][j - 1] + 1;
}
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
if (word1[i] == word2[j])
dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]) + 1, dp[i - 1][j - 1]);
else
dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
return dp[m - 1][n - 1];
}
};
```
### B
```c
class Solution
{
public:
int minDistance(string word1, string word2)
{
int n = word1.size();
int m = word2.size();
if (n * m == 0)
{
return n + m;
}
int d[n + 1][m + 1];
for (int i = 0; i < n + 1; ++i)
{
d[i][0] = i;
}
for (int i = 0; i < m + 1; ++i)
{
d[0][i] = i;
}
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];
}
};
```
### C
```c
class Solution
{
public:
int minDistance(string word1, string word2)
{
int m = word1.size(), n = word2.size();
vector> dp(m + 1, vector(n + 1, 0));
for (int i = 1; i <= n; ++i)
dp[0][i] = dp[0][i - 1] + 1;
for (int i = 1; i <= m; ++i)
dp[i][0] = dp[i - 1][0] + 1;
for (int i = 1; i <= m; ++i)
{
for (int j = 1; j <= n; ++j)
{
if (word1[i - 1] == word2[j - 1])
dp[i][j] = dp[i - 1][j - 1];
else
dp[i][j] = min(min(dp[i - 1][j - 1], dp[i - 1][j]), dp[i][j - 1]) + 1;
}
}
return dp[m][n];
}
};
```