-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditdistance2.c
104 lines (92 loc) · 2.71 KB
/
Editdistance2.c
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
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
int min(int a, int b, int c) {
int min = a;
if (b < min) {
min = b;
}
if (c < min) {
min = c;
}
return min;
}
int editDistance(char str1[], char str2[], int len1, int len2) {
int dp[MAX_LEN][MAX_LEN];
int i, j;
for (i = 0; i <= len1; i++) {
for (j = 0; j <= len2; j++) {
if (i == 0) {
dp[i][j] = j;
} else if (j == 0) {
dp[i][j] = i;
} else if (str1[i - 1] == str2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]);
}
}
}
// print the steps to convert str1 to str2
i = len1;
j = len2;
while (i > 0 && j > 0) {
if (str1[i - 1] == str2[j - 1]) {
i--;
j--;
} else if (dp[i][j] == dp[i - 1][j - 1] + 1) {
printf("Replace %c with %c\n", str1[i - 1], str2[j - 1]);
str1[i - 1] = str2[j - 1];
i--;
j--;
} else if (dp[i][j] == dp[i - 1][j] + 1) {
printf("Delete %c\n", str1[i - 1]);
i--;
// delete character from str1
memmove(&str1[i], &str1[i + 1], len1 - i);
len1--;
} else if (dp[i][j] == dp[i][j - 1] + 1) {
printf("Insert %c\n", str2[j - 1]);
// insert character into str1
memmove(&str1[i], &str1[i - 1], len1 - i + 1);
str1[i - 1] = str2[j - 1];
len1++;
j--;
}
printf("Updated string 1: %s\n", str1);
printf("Updated string 2: %s\n", str2);
}
while (i > 0) {
printf("Delete %c\n", str1[i - 1]);
i--;
// delete character from str1
memmove(&str1[i], &str1[i + 1], len1 - i);
len1--;
printf("Updated string 1: %s\n", str1);
printf("Updated string 2: %s\n", str2);
}
while (j > 0) {
printf("Insert %c\n", str2[j - 1]);
// insert character into str1
memmove(&str1[i], &str1[i - 1], len1 - i);
str1[i - 1] = str2[j - 1];
len1++;
j--;
printf("Updated string 1: %s\n", str1);
printf("Updated string 2: %s\n", str2);
}
return dp[len1][len2];
}
int main() {
char str1[MAX_LEN], str2[MAX_LEN];
int len1, len2, distance;
printf("Enter string 1: ");
scanf("%s", str1);
printf("Enter string 2: ");
scanf("%s", str2);
len1 = strlen(str1);
len2 = strlen(str2);
distance = editDistance(str1, str2, len1, len2);
printf("Minimum edit distance: %d\n", distance);
return 0;
}