-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_distance.cpp
More file actions
44 lines (41 loc) · 1.51 KB
/
Copy pathedit_distance.cpp
File metadata and controls
44 lines (41 loc) · 1.51 KB
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
class Solution {
public:
/**
* @param word1 & word2: Two string.
* @return: The minimum number of steps.
*/
int minDistance(string word1, string word2) {
// write your code here
// f[i][j] i characters in word1, j characters in word2 min distance
// f[0][j] = j;
// f[i][0] = i;
// last status to f[i][j]
// insert: f[i][j] = f[i][j - 1] + 1;
// replace: f[i][j] = f[i - 1]f[j - 1] + 1
//f[i - 1][j - 1] if word1[i + 1] == word2[j + 1]
// delele: f[i][j] = f[i - 1][j] + 1
// hint:当前状态的前一步是怎样的,怎样由前一步的状态转化为当前的状态
int len_word1 = word1.size();
int len_word2 = word2.size();
vector<vector<int>> f(len_word1 + 1, vector<int>(len_word2 + 1, 0));
for (int i = 1; i < len_word1 + 1; i++) {
f[i][0] = i;
}
for (int j = 1; j < len_word2 + 1; j++) {
f[0][j] = j;
}
for (int i = 1; i < len_word1 + 1; i++) {
for (int j = 1; j < len_word2 + 1; j++) {
int temp1 = f[i][j - 1] + 1;
int temp2 = f[i - 1][j - 1] + 1;
if (word1[i - 1] == word2[j - 1]) {
temp2 -= 1;
}
int temp3 = f[i - 1][j] + 1;
temp1 = min(temp1, temp2);
f[i][j] = min(temp1, temp3);
}
}
return f[len_word1][len_word2];
}
};