1
1
/**
2
2
* @file
3
- * @brief [Longest Common Subsequence](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem) algorithm
3
+ * @brief [Longest Common
4
+ * Subsequence](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem)
5
+ * algorithm
4
6
* @details
5
7
* From Wikipedia: The longest common subsequence (LCS) problem is the problem
6
- * of finding the longest subsequence common to all sequences in a set of sequences
7
- * (often just two sequences).
8
+ * of finding the longest subsequence common to all sequences in a set of
9
+ * sequences (often just two sequences).
8
10
* @author [Kurtz](https://github.com/itskurtz)
9
11
*/
12
+
10
13
#include <stdio.h> /* for io operations */
11
14
#include <stdlib.h> /* for memory management & exit */
12
15
#include <string.h> /* for string manipulation & ooperations */
15
18
enum {LEFT , UP , DIAG };
16
19
17
20
/**
18
- * @breif Computes LCS between s1 and s2 using a dynamic-programming approach
19
- * @param1 s1 first null-terminated string
20
- * @param2 s2 second null-terminated string
21
- * @param3 l1 length of s1
22
- * @param4 l2 length of s2
23
- * @param5 L matrix of size l1 x l2
24
- * @param6 B matrix of size l1 x l2
21
+ * @brief Computes LCS between s1 and s2 using a dynamic-programming approach
22
+ * @param s1 first null-terminated string
23
+ * @param s2 second null-terminated string
24
+ * @param l1 length of s1
25
+ * @param l2 length of s2
26
+ * @param L matrix of size l1 x l2
27
+ * @param B matrix of size l1 x l2
25
28
* @returns void
26
29
*/
27
30
void lcslen (const char * s1 , const char * s2 , int l1 , int l2 , int * * L , int * * B ) {
@@ -31,8 +34,8 @@ void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) {
31
34
32
35
/* loop over the simbols in my sequences
33
36
save the directions according to the LCS */
34
- for (i = 1 ; i <= l1 ; ++ i )
35
- for (j = 1 ; j <= l2 ; ++ j )
37
+ for (i = 1 ; i <= l1 ; ++ i ) {
38
+ for (j = 1 ; j <= l2 ; ++ j ) {
36
39
if (s1 [i - 1 ] == s2 [j - 1 ]) {
37
40
L [i ][j ] = 1 + L [i - 1 ][j - 1 ];
38
41
B [i ][j ] = DIAG ;
@@ -44,16 +47,18 @@ void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) {
44
47
else {
45
48
L [i ][j ] = L [i - 1 ][j ];
46
49
B [i ][j ] = UP ;
47
- }
50
+ }
51
+ }
52
+ }
48
53
}
49
54
50
55
/**
51
- * @breif Builds the LCS according to B using a traceback approach
52
- * @param1 s1 first null-terminated string
53
- * @param2 l1 length of s1
54
- * @param3 l2 length of s2
55
- * @param4 L matrix of size l1 x l2
56
- * @param5 B matrix of size l1 x l2
56
+ * @brief Builds the LCS according to B using a traceback approach
57
+ * @param s1 first null-terminated string
58
+ * @param l1 length of s1
59
+ * @param l2 length of s2
60
+ * @param L matrix of size l1 x l2
61
+ * @param B matrix of size l1 x l2
57
62
* @returns lcs longest common subsequence
58
63
*/
59
64
char * lcsbuild (const char * s1 , int l1 , int l2 , int * * L , int * * B ) {
@@ -76,13 +81,18 @@ char *lcsbuild(const char *s1, int l1, int l2, int **L, int **B) {
76
81
i = i - 1 ;
77
82
j = j - 1 ;
78
83
}
79
- else if (B [i ][j ] == LEFT )
80
- j = j - 1 ;
81
- else
82
- i = i - 1 ;
84
+ else if (B [i ][j ] == LEFT )
85
+ {
86
+ j = j - 1 ;
87
+ }
88
+ else
89
+ {
90
+ i = i - 1 ;
91
+ }
83
92
}
84
93
return lcs ;
85
94
}
95
+
86
96
/**
87
97
* @brief Self-test implementations
88
98
* @returns void
@@ -132,9 +142,11 @@ static void test() {
132
142
printf ("LCS len:%3d\n" , L [l1 ][l2 ]);
133
143
printf ("LCS: %s\n" , lcs );
134
144
135
- free (lcs );
136
- for (j = 0 ; j <= l1 ; j ++ )
137
- free (L [j ]), free (B [j ]);
145
+ free (lcs );
146
+ for (j = 0 ; j <= l1 ; j ++ )
147
+ {
148
+ free (L [j ]), free (B [j ]);
149
+ }
138
150
free (L );
139
151
free (B );
140
152
0 commit comments