solution.md 5.2 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 编辑距离
F
fix bug  
feilong 已提交
2

每日一练社区's avatar
每日一练社区 已提交
3
<p>给你两个单词 <code>word1</code> 和 <code>word2</code>,请你计算出将 <code>word1</code> 转换成 <code>word2</code><em> </em>所使用的最少操作数 。</p><p>你可以对一个单词进行如下三种操作:</p><ul>	<li>插入一个字符</li>	<li>删除一个字符</li>	<li>替换一个字符</li></ul><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>word1 = "horse", word2 = "ros"<strong><br />输出:</strong>3<strong><br />解释:</strong>horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>word1 = "intention", word2 = "execution"<strong><br />输出:</strong>5<strong><br />解释:</strong>intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>0 <= word1.length, word2.length <= 500</code></li>	<li><code>word1</code><code>word2</code> 由小写英文字母组成</li></ul>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
5

每日一练社区's avatar
每日一练社区 已提交
6
## aop
F
fix bug  
feilong 已提交
7

每日一练社区's avatar
每日一练社区 已提交
8
### before
F
fix bug  
feilong 已提交
9

每日一练社区's avatar
每日一练社区 已提交
10 11 12 13 14
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
15

每日一练社区's avatar
每日一练社区 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
```cpp
int main()
{
    Solution sol;

    string word1 = "horse";
    string word2 = "ros";

    int res;
    res = sol.minDistance(word1, word2);
    cout << res;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
32

每日一练社区's avatar
每日一练社区 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
```cpp
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];
    }
};
```
## 选项

F
fix bug  
feilong 已提交
64

每日一练社区's avatar
每日一练社区 已提交
65
### A
F
fix bug  
feilong 已提交
66

每日一练社区's avatar
每日一练社区 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
```cpp
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
F
fix bug  
feilong 已提交
123

每日一练社区's avatar
每日一练社区 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
```cpp
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
F
fix bug  
feilong 已提交
168

每日一练社区's avatar
每日一练社区 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
```cpp
class Solution
{
public:
    int minDistance(string word1, string word2)
    {
        int m = word1.size(), n = word2.size();

        vector<vector<int>> dp(m + 1, vector<int>(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];
    }
};
```