-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_distance_LCS.txt
65 lines (53 loc) · 1.49 KB
/
edit_distance_LCS.txt
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
int cache[1000][1000];
int check_str(char *text1, char *text2, int t1, int t2, int t1_len, int t2_len)
{
int count = 0;
int lt = t1;
int rt = t2;
if (cache[t1][t2] != -1)
return cache[t1][t2];
while(t1 < t1_len && t2 < t2_len) {
if (text1[t1] == text2[t2]) {
t1++;
t2++;
count++;
} else {
int l = check_str(text1, text2, t1 + 1, t2, t1_len, t2_len);
int r = check_str(text1, text2, t1, t2 + 1, t1_len, t2_len);
if (l > r) {
cache[lt][rt] = l + count;
return l + count;
} else {
cache[lt][rt] = r + count;
return r + count;
}
}
}
cache[lt][rt] = count;
return count;
}
int longestCommonSubsequence(char * text1, char * text2){
int t1 = 0;
int t2 = 0;
int t1_len = strlen(text1);
int t2_len = strlen(text2);
int count = 0;
memset(cache, -1, sizeof(int) * 500 * 500);
if (t2_len < t1_len) {
char *temp = text1;
text1 = text2;
text2 = temp;
int len = t1_len;
t1_len = t2_len;
t2_len = len;
}
count = check_str(text1, text2, t1, t2, t1_len, t2_len);
return count;
}
int minDistance(char * word1, char * word2){
int first = longestCommonSubsequence(word1, word2);
int del = 0;
del += strlen(word1) - first;
del += strlen(word2) - first;
return del;
}