Mercurial > hg > toys
changeset 0:57fc6080db4d
Implement levenshtein distance
author | Jordi Gutiérrez Hermoso <jordigh@gmail.com> |
---|---|
date | Thu, 16 Dec 2010 21:09:33 -0600 |
parents | |
children | 60954ea75cce |
files | levenshtein.c++ |
diffstat | 1 files changed, 33 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/levenshtein.c++ @@ -0,0 +1,33 @@ +#include <string> +#include <vector> + +using namespace std; + +size_t min(size_t x, size_t y) +{ + return x < y ? x : y; +} + + +size_t levenshtein(const string& a, + const string& b) +{ + vector<vector<size_t>> matrix(a.size(), vector<size_t>(b.size(), 0)); + + for(size_t i = 0; i < a.size(); i++) + { + matrix[i][0] = i; + for(size_t j = 0; j < b.size(); j++) + { + if(i == 0) + { + matrix[0][j] = j; + continue; + } + matrix[i][j] = min( matrix[i-1][j-1] + size_t(a[i] != b[j]), + 1 + min(matrix[i-1][j], + matrix[i][j-1])); + } + } + return matrix[a.size()-1][b.size()-1]; +}