Advertisement
shabbyheart

lcs using recursinon

Jan 12th, 2020
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int check=0;
  5. int lcs(string X, string Y, int m, int n)
  6. {
  7.     check++;
  8.     if (m == 0 || n == 0)
  9.         return 0;
  10.  
  11.     if (X[m - 1] == Y[n - 1])
  12.         return 1 + lcs(X, Y, m - 1, n - 1);
  13.     else
  14.         return max(lcs(X, Y, m, n - 1),
  15.                 lcs(X, Y, m - 1, n));
  16. }
  17.  
  18.  
  19. int main()
  20. {
  21.     string X = "AGGTAB";
  22.     string Y = "GXTXAYB";
  23.  
  24.     int m = X.length();
  25.     int n = Y.length();
  26.  
  27.     cout << "Length of LCS: " << lcs(X, Y, m, n);
  28.     cout<<endl<<check<<endl;
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement