-
Notifications
You must be signed in to change notification settings - Fork 985
/
Copy pathedit_Distance.cpp
108 lines (90 loc) · 2.25 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Task. The goal of this problem is to implement the algorithm for computing the edit distance between two
strings.
Input Format. Each of the two lines of the input contains a string consisting of lower case latin letters.
Constraints. The length of both strings is at least 1 and at most 100.
Output Format. Output the edit distance between the given two strings.
//////////
Sample 1.
Input:
ab
ab
Output:
0
//////////
Sample 2.
Input:
short
ports
Output:
3
*/
#include <bits/stdc++.h>
using namespace std;
int minDis(string s1, string s2, int n, int m, vector<vector<int>> &dp)
{
// If any string is empty,
// return the remaining characters of other string
if (n == 0)
return m;
if (m == 0)
return n;
// To check if the recursive tree
// for given n & m has already been executed
if (dp[n][m] != -1)
return dp[n][m];
// If characters are equal, execute
// recursive function for n-1, m-1
if (s1[n - 1] == s2[m - 1])
{
if (dp[n - 1][m - 1] == -1)
{
return dp[n][m] = minDis(s1, s2, n - 1, m - 1, dp);
}
else
return dp[n][m] = dp[n - 1][m - 1];
}
// If characters are nt equal, we need to
// find the minimum cost out of all 3 operations.
else
{
int m1, m2, m3; // temp variables
if (dp[n - 1][m] != -1)
{
m1 = dp[n - 1][m];
}
else
{
m1 = minDis(s1, s2, n - 1, m, dp);
}
if (dp[n][m - 1] != -1)
{
m2 = dp[n][m - 1];
}
else
{
m2 = minDis(s1, s2, n, m - 1, dp);
}
if (dp[n - 1][m - 1] != -1)
{
m3 = dp[n - 1][m - 1];
}
else
{
m3 = minDis(s1, s2, n - 1, m - 1, dp);
}
return dp[n][m] = 1 + min(m1, min(m2, m3));
}
}
// Driver program
int main()
{
string str1;
string str2;
cin >> str1;
cin >> str2;
int n = str1.length(), m = str2.length();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, -1));
cout << minDis(str1, str2, n, m, dp);
return 0;
}