diff --git a/CMakeLists.txt b/CMakeLists.txt index eb925dc9..29fb9d20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,7 @@ add_subdirectory(process_scheduling_algorithms) add_subdirectory(numerical_methods) add_subdirectory(math) add_subdirectory(cipher) +add_subdirectory(dynamic_programming) ## Configure Doxygen documentation system cmake_policy(SET CMP0054 NEW) diff --git a/dynamic_programming/CMakeLists.txt b/dynamic_programming/CMakeLists.txt new file mode 100644 index 00000000..a65bbb7d --- /dev/null +++ b/dynamic_programming/CMakeLists.txt @@ -0,0 +1,18 @@ +# If necessary, use the RELATIVE flag, otherwise each source file may be listed +# with full pathname. The RELATIVE flag makes it easier to extract an executable's name +# automatically. + +file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c ) +foreach( testsourcefile ${APP_SOURCES} ) + string( REPLACE ".c" "" testname ${testsourcefile} ) # File type. Example: `.c` + add_executable( ${testname} ${testsourcefile} ) + + if(OpenMP_C_FOUND) + target_link_libraries(${testname} OpenMP::OpenMP_C) + endif() + if(MATH_LIBRARY) + target_link_libraries(${testname} ${MATH_LIBRARY}) + endif() + install(TARGETS ${testname} DESTINATION "bin/dynamic_programming") # Folder name. Do NOT include `<>` + +endforeach( testsourcefile ${APP_SOURCES} ) diff --git a/dynamic_programming/lcs.c b/dynamic_programming/lcs.c index 5637a72d..516fa48f 100644 --- a/dynamic_programming/lcs.c +++ b/dynamic_programming/lcs.c @@ -31,8 +31,8 @@ void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) { /* loop over the simbols in my sequences save the directions according to the LCS */ - for (i = 1; i <= l1; ++i) - for (j = 1; j <= l2; ++j) + for (i = 1; i <= l1; ++i) { + for (j = 1; j <= l2; ++j) { if (s1[i-1] == s2[j-1]) { L[i][j] = 1 + L[i-1][j-1]; B[i][j] = DIAG; @@ -44,7 +44,9 @@ void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) { else { L[i][j] = L[i-1][j]; B[i][j] = UP; - } + } + } + } } /** @@ -76,10 +78,12 @@ char *lcsbuild(const char *s1, int l1, int l2, int **L, int **B) { i = i - 1; j = j - 1; } - else if (B[i][j] == LEFT) - j = j - 1; - else - i = i - 1; + else if (B[i][j] == LEFT) { + j = j - 1; + } + else { + i = i - 1; + } } return lcs; } @@ -133,8 +137,9 @@ static void test() { printf("LCS: %s\n", lcs); free(lcs); - for (j = 0; j <= l1; j++) - free(L[j]), free(B[j]); + for (j = 0; j <= l1; j++) { + free(L[j]), free(B[j]); + } free(L); free(B);