forked from MaskRay/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
edit-distance.cc
40 lines (36 loc) · 916 Bytes
/
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
// Edit Distance
#define REP(i, n) for (int i = 0; i < (n); i++)
class Solution {
public:
int minDistance(string a, string b) {
vector<vector<int>> f(2, vector<int>(b.size()+1));
iota(f[0].begin(), f[0].end(), 0);
REP(i, a.size()) {
f[i+1&1][0] = i+1;
REP(j, b.size())
f[i+1&1][j+1] = a[i] == b[j] ? f[i&1][j] : min(min(f[i&1][j], f[i&1][j+1]), f[i+1&1][j]) + 1;
}
return f[a.size()&1][b.size()];
}
};
///
#define REP(i, n) for (int i = 0; i < (n); i++)
class Solution {
public:
int minDistance(string a, string b) {
if (a.size() < b.size())
swap(a, b);
vector<int> d(b.size()+1);
iota(d.begin(), d.end(), 0);
REP(i, a.size()) {
int ul = d[0];
d[0] = i+1;
REP(j, b.size()) {
int t = d[j+1];
d[j+1] = a[i] == b[j] ? ul : min(ul, min(d[j], d[j+1])) + 1;
ul = t;
}
}
return d.back();
}
};