1
+ /*
2
+ * Note: This template uses some c++11 functions , so you have to compile it
3
+ * with c++11 flag. Example:- $ g++ -std=c++11 c++Template.cpp
4
+ *
5
+ * Author : Harsh Gupta
6
+ * Handle: harshsavasil
7
+ *
8
+ */
9
+
10
+ /*
11
+ Problem Description: Minimum Edit Distance
12
+ Given two strings str1 and str2 and below operations that can performed on str1.
13
+ Find minimum number of edits (operations) required to convert ‘str1’ into
14
+ ‘str2’. Operations - Insert Remove Replace All of the above operations are of
15
+ equal cost.
16
+
17
+
18
+ Tags: Strings, Dynamic Programming, Distance Measure
19
+
20
+ Sample Input -
21
+ str1 = "geek", str2 = "gesek"
22
+
23
+ Sample Output -
24
+ 1
25
+
26
+ Explanation - We can convert str1 into str2 by inserting a 's'.
27
+ */
28
+ #include < algorithm>
29
+ #include < cctype>
30
+ #include < cmath>
31
+ #include < cstdio>
32
+ #include < cstring>
33
+ #include < functional>
34
+ #include < iostream>
35
+ #include < list>
36
+ #include < map>
37
+ #include < queue>
38
+ #include < set>
39
+ #include < sstream>
40
+ #include < stack>
41
+ #include < string>
42
+ #include < vector>
43
+
44
+ using namespace std ;
45
+
46
+ #define DEBUG (x ) cout << ' >' << #x << ' :' << x << endl;
47
+ #define REP (i, n ) for (int i = 0 ; i < (n); i++)
48
+ #define FOR (i, a, b ) for (int i = (a); i <= (b); i++)
49
+ #define FORD (i, a, b ) for (int i = (a); i >= (b); i--)
50
+ inline bool EQ (double a, double b) { return fabs (a - b) < 1e-9 ; }
51
+ const int INF = 1 << 29 ;
52
+ typedef long long ll;
53
+ typedef pair<int , int > ii;
54
+ typedef vector<ii> vii;
55
+ typedef vector<int > vi;
56
+ typedef vector<vector<int >> vvi;
57
+ inline int two (int n) { return 1 << n; }
58
+ inline int test (int n, int b) { return (n >> b) & 1 ; }
59
+ inline void set_bit (int &n, int b) { n |= two (b); }
60
+ inline void unset_bit (int &n, int b) { n &= ~two (b); }
61
+ inline int last_bit (int n) { return n & (-n); }
62
+ inline int ones (int n) {
63
+ int res = 0 ;
64
+ while (n && ++res) n -= n & (-n);
65
+ return res;
66
+ }
67
+ template <class T >
68
+ void chmax (T &a, const T &b) {
69
+ a = max (a, b);
70
+ }
71
+ template <class T >
72
+ void chmin (T &a, const T &b) {
73
+ a = min (a, b);
74
+ }
75
+
76
+ // ///////////////////////////////////////////////////////////////////
77
+ int main () {
78
+ freopen (" input.txt" , " r" , stdin);
79
+ freopen (" output.txt" , " w" , stdout);
80
+ int tc;
81
+ cin >> tc;
82
+ while (tc--) {
83
+ string a, b;
84
+ cin >> a >> b;
85
+ int a_size = a.size ();
86
+ int b_size = b.size ();
87
+ vvi distance (a_size + 1 , vi (b_size + 1 , -1 ));
88
+ distance[0 ][0 ] = 0 ;
89
+ for (int i = 0 ; i <= a_size; i++) {
90
+ for (int j = 0 ; j <= b_size; j++) {
91
+ if (i == 0 && j == 0 ) {
92
+ distance[i][j] = 0 ;
93
+ } else if (i == 0 ) {
94
+ distance[i][j] = j;
95
+ } else if (j == 0 ) {
96
+ distance[i][j] = i;
97
+ } else {
98
+ if (a[i - 1 ] == b[j - 1 ]) {
99
+ distance[i][j] = distance[i - 1 ][j - 1 ];
100
+ } else {
101
+ distance[i][j] = min (distance[i - 1 ][j - 1 ], min (distance[i - 1 ][j], distance[i][j - 1 ])) + 1 ;
102
+ }
103
+ }
104
+ }
105
+ }
106
+ cout << " Edit Distance between " << a << " and " << b << " is " << distance[a_size][b_size] << endl;
107
+ }
108
+ return 0 ;
109
+ }
0 commit comments