jain12

longest common subsequence by recursion

May 4th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. #include<iostream>
  2. #include<math.h>
  3. #include<cstring>
  4. using namespace std;
  5.  
  6. int LCS(char str1[],char str2[],int l1,int l2){
  7.   if(l1==0 ||l2==0)
  8.      return 0;
  9.   if(str1[l1-1]==str2[l2-1])
  10.   return 1+LCS(str1,str2,l1-1,l2-1);
  11.   return max(LCS(str1,str2,l1,l2-1),LCS(str1,str2,l1-1,l2));
  12.   }
  13.  
  14. int main(){
  15.   char X[] = "AGGTAB";
  16.   char Y[] = "GXTXAYB";
  17.   int m = strlen(X);
  18.   int n = strlen(Y);
  19.   cout<<"Length of LCS is "<<LCS( X, Y, m, n ) ;
  20.   return 0;
  21.   }
Add Comment
Please, Sign In to add comment