-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit-distance.cc
82 lines (70 loc) · 1.67 KB
/
edit-distance.cc
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// https://oj.leetcode.com/problems/edit-distance/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.size();
int n = word2.size();
// dp[i][j]为word1[0...i-1](长度为i)和word2[0...j-1](长度为j)的编辑距离
// dp[0][0]=0
// dp[i][0]=i
// dp[0][j]=j
// dp[i][j] = dp[i-1][j-1] if word1[i-1]==word2[j-1]
// min {
// dp[i-1][j] + 1, // word1[0...i-2]->word2[0...j-1]再添加word1[i-1]
// dp[i][j-1] + 1, // word1[0...i-1]->word2[0...j-2]再添加word2[j-1]
// dp[i-1][j-1] + 1, // word1[0...i-2]->word2[0...j-2]再把word1[i-1]替换为word2[j-1]
// }
// dp[m][n]即为所求
#if 1
vector<int> dp(n + 1);
for (int j = 0; j <= n; j++) {
dp[j] = j;
}
int saved; // 保存dp[i-1[j-1]
for (int i = 1; i <= m; i++) {
saved = dp[0];
dp[0] = i;
for (int j = 1; j <= n; j++) {
int tmp = dp[j];
if (word1[i-1] == word2[j-1]) {
dp[j] = saved;
} else {
dp[j] = 1 + min(min(dp[j], dp[j-1]), saved);
}
saved = tmp;
}
}
return dp[n];
#else
vector<vector<int> > dp(m + 1, vector<int>(n + 1));
for (int i = 0; i <= m; i++) {
dp[i][0]=i;
}
for (int j = 1; 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] = 1 + min(min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]);
}
}
}
return dp[m][n];
#endif
}
};
int main(int argc, char const* argv[])
{
string a = "a";
string b = "a";
Solution s;
cout << s.minDistance(a, b) << endl;
return 0;
}