Advertisement
Psycho_Coder

Longest Common Subsequence Using Recursion

Oct 10th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int max(int a, int b) {
  5.     return (a > b)? a : b;
  6. }
  7.  
  8. /* Returns length of LCS for X[0..m-1], Y[0..n-1] */
  9. int lcs( char *X, char *Y, int m, int n ) {
  10.    if (m == 0 || n == 0)
  11.      return 0;
  12.    if (X[m-1] == Y[n-1])
  13.      return 1 + lcs(X, Y, m-1, n-1);
  14.    else
  15.      return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
  16. }
  17.  
  18. int main() {
  19.   char X[] = "GGXATAB";
  20.   char Y[] = "GXTXAYB";
  21.  
  22.   int m = strlen(X);
  23.   int n = strlen(Y);
  24.  
  25.   printf("Length of LCS is %d\n", lcs( X, Y, m, n ) );
  26.  
  27.   return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement