-
Notifications
You must be signed in to change notification settings - Fork 4
/
edit_distance.cpp
41 lines (37 loc) · 967 Bytes
/
edit_distance.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
/**
* Author: Skylar Payne
* Date: December 26, 2014
* Calculate the edit distance between two strings
**/
#include <iostream>
#include <vector>
#include <algorithm>
int edit_distance(std::string const& a, std::string const& b) {
std::vector<std::vector<int> > res(a.size() + 1, std::vector<int>(b.size() + 1));
for(int i = 0; i < res.size(); ++i) {
res[i][0] = i;
}
for(int i = 0; i < res[0].size(); ++i) {
res[0][i] = i;
}
for(int i = 1; i < res.size(); ++i) {
for(int j = 1; j < res[0].size(); ++j) {
if(a[i] == b[j]) {
res[i][j] = res[i -1][j - 1];
} else {
res[i][j] = 1 + std::min(res[i -1][j], res[i][j - 1]);
}
}
}
return res[a.size()][b.size()];
}
int main(int argc, char** argv) {
if(argc != 3) {
std::cout << "Please provide 2 strings to calculate the edit distance of." << std::endl;
return -1;
}
std::string a(argv[1]);
std::string b(argv[2]);
std::cout << edit_distance(a, b) << std::endl;
return 0;
}