-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathThe Levenshtein distance (Edit distance) problem.cpp
56 lines (45 loc) · 1.39 KB
/
The Levenshtein distance (Edit distance) problem.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;
// Function to find Levenshtein Distance between string X and Y
// m and n are the number of characters in X and Y respectively
int dist(string X, int m, string Y, int n)
{
// for all i and j, T[i,j] will hold the Levenshtein distance between
// the first i characters of X and the first j characters of Y
// note that T has (m+1)*(n+1) values
int T[m + 1][n + 1];
// set each element in T to zero
memset(T, 0, sizeof(T));
// source prefixes can be transformed into empty string by
// dropping all characters
for (int i = 1; i <= m; i++)
T[i][0] = i; // (case 1)
// target prefixes can be reached from empty source prefix
// by inserting every character
for (int j = 1; j <= n; j++)
T[0][j] = j; // (case 1)
int substitutionCost;
// fill the lookup table in bottom-up manner
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (X[i - 1] == Y[j - 1]) // (case 2)
substitutionCost = 0; // (case 2)
else
substitutionCost = 1; // (case 3c)
T[i][j] = min(min(T[i - 1][j] + 1, // deletion (case 3b)
T[i][j - 1] + 1), // insertion (case 3a)
T[i - 1][j - 1] + substitutionCost); // replace (case 2 + 3c)
}
}
return T[m][n];
}
// main function
int main()
{
string X = "kitten", Y = "sitting";
cout << "The Levenshtein Distance is " <<
dist(X, X.length(), Y, Y.length());
return 0;
}