|
4 | 4 | * Source: https://github.com/dragonslayerx
|
5 | 5 | */
|
6 | 6 |
|
7 | | -#define MAX 2050 |
8 | | -int edist[2][MAX]; |
9 | | - |
10 | | -class Edist { |
11 | | - public: |
12 | | - int solve(string &S1, string &S2){ |
13 | | - int last = 0, current = 1; |
14 | | - for (int i = 0; i <= S1.size(); i++){ |
15 | | - for (int j = 0; j <= S2.size(); j++){ |
16 | | - if (i == 0) edist[current][j] = j; |
17 | | - else if (j == 0) edist[current][j] = i; |
18 | | - else { |
19 | | - edist[current][j] = min(edist[last][j] + 1, edist[current][j-1]+1); |
20 | | - if (S1[i-1] == S2[j-1]) { |
21 | | - edist[current][j] = min(edist[current][j], edist[last][j-1]); |
22 | | - } else { |
23 | | - edist[current][j] = min(edist[current][j], edist[last][j-1] + 1); |
24 | | - } |
25 | | - } |
26 | | - } |
27 | | - swap(last, current); |
28 | | - } |
29 | | - return edist[last][S2.size()]; |
30 | | - } |
31 | | -}; |
| 7 | +int d[2][1005]; |
| 8 | +int solve(string &s, string &t){ |
| 9 | + int prev = 0, current = 1; |
| 10 | + for (int i = 0; i <= s.size(); i++){ |
| 11 | + for (int j = 0; j <= t.size(); j++){ |
| 12 | + if (i == 0) d[current][j]=j; |
| 13 | + else if (j == 0) d[current][j]=i; |
| 14 | + else { |
| 15 | + d[current][j] = min(d[prev][j]+1, d[current][j-1]+1); |
| 16 | + if (s[i-1]==t[j-1]) { |
| 17 | + d[current][j] = min(d[current][j], d[prev][j-1]); |
| 18 | + } else { |
| 19 | + d[current][j] = min(d[current][j], d[prev][j-1]+1); |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + swap(prev, current); |
| 24 | + } |
| 25 | + return d[prev][t.size()]; |
| 26 | +} |
0 commit comments