-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcs.c
More file actions
105 lines (80 loc) · 2.08 KB
/
lcs.c
File metadata and controls
105 lines (80 loc) · 2.08 KB
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
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int* lcs(char arr1[], char arr2[]);
int* max(int n1, int n2);
int main(){
char arr1[] = "BDCABA";
char arr2[] = "ABCBDAB";
int* ans;
ans = lcs(arr1, arr2);
printf("The length of LCS is: %d\n", ans[0]);
printf("One of the LCS is: ");
for (int i = 0; i < ans[0]; i ++){
printf("%c", arr1[ans[ans[0]-i]-1]);
}
return 0;
}
int* max(int n1, int n2){
int* ans;
ans = (int*) malloc(2*sizeof(int));
if (n1 >= n2){
ans[0] = 0;
ans[1] = n1;
return ans;
} else {
ans[0] = 1;
ans[1] = n2;
return ans;
}
return 0;
}
int* lcs(char arr1[], char arr2[]){
int n1, n2;
n1 = strlen(arr1) + 1;
n2 = strlen(arr2) + 1;
int **m, **anc;
m = (int**) calloc(n1, sizeof(int*));
for (int i = 0; i < n1; i ++){
m[i] = (int*) calloc(n2, sizeof(int));
}
anc = (int**) calloc(n1, sizeof(int*));
for (int i = 0; i < n1; i ++){
anc[i] = (int*) calloc(n2, sizeof(int));
}
for (int i = 1; i < n1; i ++){
for (int j = 1; j < n2; j ++){
if (arr1[i-1] == arr2[j-1]){
m[i][j] = m[i-1][j-1] + 1;
anc[i][j] = 3;
} else {
int *k;
k = max(m[i-1][j], m[i][j-1]);
m[i][j] = k[1];
if (k[0] == 0){
anc[i][j] = 1;
} else {
anc[i][j] = 2;
}
}
}
}
int* inx;
inx = (int*) malloc(sizeof(int) * 50);
int count = 0;
int posi = n1 - 1, posj = n2 - 1;
while (anc[posi][posj] != 0){
if (anc[posi][posj] == 1){
posi -= 1;
} else if (anc[posi][posj] == 2){
posj -= 1;
} else {
inx[count + 1] = posi;
count ++;
posi -= 1;
posj -= 1;
}
}
inx[0] = count;
return inx;
}