-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25ee928
commit 93a34d1
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
|
||
} |