-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path27.EditDistance.cpp
47 lines (36 loc) · 884 Bytes
/
27.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
43
44
45
46
47
/*
* This program takes two strings from the user and
* calculates Edit distance between them using
* Dynamic Prograrmming
*
* Coded by: Abdurrezak Efe
*
* */
#include <iostream>
#include <cmath>
using namespace std;
int edit_distance(string s1, string s2, int m, int n)
{
int d[m+1][n+1]; //our bottom up table
for (int i=0; i <= m; i++)
{
for (int j=0; j <= n; j++)
{
if (i==0)
d[i][j] = j;
else if (j==0)
d[i][j] = i;
else if (s1[i-1] == s2[j-1]) //no op needed
d[i][j] = d[i-1][j-1];
else
d[i][j] = 1 + min(min(d[i][j-1], d[i-1][j]), d[i-1][j-1]); //insert delete
}
}
return d[m][n];
}
int main()
{
string s1,s2;
cin >> s1 >> s2;
cout << edit_distance(s1,s2,s1.size(),s2.size()) << endl;
}