-
Notifications
You must be signed in to change notification settings - Fork 0
/
Edit Distance.cpp
42 lines (37 loc) · 1.01 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
#include<bits/stdc++.h>
using namespace std;
void editDistance(string, string, int, int);
int main(){
int t, p, q;
string s1, s2;
cin >> t;
while(t--){
cin >> p >> q >> s1 >> s2;
editDistance(s1, s2, p, q);
cout << endl;
}
return 0;
}
void editDistance(string s1, string s2, int l1, int l2){
int arr[l1 + 1][l2 + 1];
for(int i = 0; i <= l1; i++)
arr[i][0] = i;
for(int i = 0; i <= l2; i++)
arr[0][i] = i;
for(int i = 1; i <= l1; i++)
for(int j = 1; j <= l2; j++){
if(s1[i - 1] == s2[j - 1])
arr[i][j] = arr[i - 1][j - 1];
else{
int min = INT_MAX;
if(min > arr[i][j - 1])
min = arr[i][j - 1];
if(min > arr[i - 1][j])
min = arr[i - 1][j];
if(min > arr[i - 1][j - 1])
min = arr[i - 1][j - 1];
arr[i][j] = 1 + min;
}
}
cout << arr[l1][l2];
}