11/**
22 * @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
46 * @details
57 * 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).
810 * @author [Kurtz](https://github.com/itskurtz)
911 */
12+
1013#include <stdio.h> /* for io operations */
1114#include <stdlib.h> /* for memory management & exit */
1215#include <string.h> /* for string manipulation & ooperations */
1518enum {LEFT , UP , DIAG };
1619
1720/**
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
2528 * @returns void
2629 */
2730void 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) {
3134
3235 /* loop over the simbols in my sequences
3336 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 ) {
3639 if (s1 [i - 1 ] == s2 [j - 1 ]) {
3740 L [i ][j ] = 1 + L [i - 1 ][j - 1 ];
3841 B [i ][j ] = DIAG ;
@@ -44,16 +47,18 @@ void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) {
4447 else {
4548 L [i ][j ] = L [i - 1 ][j ];
4649 B [i ][j ] = UP ;
47- }
50+ }
51+ }
52+ }
4853}
4954
5055/**
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
5762 * @returns lcs longest common subsequence
5863 */
5964char * 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) {
7681 i = i - 1 ;
7782 j = j - 1 ;
7883 }
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+ }
8392 }
8493 return lcs ;
8594}
95+
8696/**
8797 * @brief Self-test implementations
8898 * @returns void
@@ -132,9 +142,11 @@ static void test() {
132142 printf ("LCS len:%3d\n" , L [l1 ][l2 ]);
133143 printf ("LCS: %s\n" , lcs );
134144
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+ }
138150 free (L );
139151 free (B );
140152
0 commit comments