-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path72.cpp
61 lines (54 loc) · 1.67 KB
/
72.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.length(), n = word2.length();
// i,j 初始化指向最后一个索引
return dp(word1, m - 1, word2, n - 1);
}
// 定义:返回 s1[0..i] 和 s2[0..j] 的最小编辑距离
int dp(string s1, int i, string s2, int j) {
// base case
if (i == -1) return j + 1;
if (j == -1) return i + 1;
if (s1[i] == s2[j]) {
return dp(s1, i - 1, s2, j - 1); // 啥都不做
}
return min0(
dp(s1, i, s2, j - 1) + 1, // 插入
dp(s1, i - 1, s2, j) + 1, // 删除
dp(s1, i - 1, s2, j - 1) + 1 // 替换
);
}
int min0(int a, int b, int c) {
return min(min(a, b), c);
}
};
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.length(), n = word2.length();
int dp[m + 1][n + 1];
for (int i = 0; i <= m; i++)
dp[i][0] = i;
for (int j = 0; j <= n; j++)
dp[0][j] = j;
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] = min0(
dp[i - 1][j] + 1,
dp[i][j - 1] + 1,
dp[i - 1][j - 1] + 1
);
}
}
}
// 储存着整个 s1 和 s2 的最小编辑距离
return dp[m][n];
}
int min0(int a, int b, int c) {
return min(a, min(b, c));
}
};