-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditdistance.cpp
42 lines (38 loc) · 965 Bytes
/
editdistance.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
#include "editdistance.h"
#include <string>
#include <algorithm>
EditDistance::EditDistance() {
//Do Nothing
}
EditDistance::EditDistance(std::string A, std::string B) {
int M = A.size(), N = B.size();
E = new int*[M + 1];
sizI = M; sizJ = N;
for(int i = 0; i <= M; ++i) {
E [i] = new int[N + 1];
}
for(int j = 0; j <= N; ++j) {
E [0][j] = j;
}
for(int i = 1; i <= M; ++i) {
E [i][0] = i;
for(int j = 1; j <= N; ++j) {
if(A[i-1] == B[j-1]) {
E [i][j] = std::min(E [i-1][j] + 1, std::min(E [i][j-1] + 1,E [i-1][j-1]));
}
else {
E [i][j] = std::min(E [i-1][j] + 1,std::min(E [i][j-1] + 1, E [i-1][j-1] + 1));
}
}
}
edit_dist = E [M][N];
}
EditDistance::~EditDistance()
{
for(int i = 0; i <= sizI; ++i) {
delete [] E [i];
}
}
int EditDistance::getDistance() {
return edit_dist;
}