Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Create Longest-Subsquence-problem.cpp #697

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions algorithm/Longest-Subsquence-problem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* A Naive recursive implementation of LCS problem */
#include <bits/stdc++.h>
using namespace std;



/* Returns length of LCS for X[0..m-1], Y[0..n-1] */
int lcs( char *X, char *Y, int m, int n )
{
if (m == 0 || n == 0)
return 0;
if (X[m-1] == Y[n-1])
return 1 + lcs(X, Y, m-1, n-1);
else
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
}



/* Driver code */
int main()
{
char X[] = "AGGTAB";
char Y[] = "GXTXAYB";

int m = strlen(X);
int n = strlen(Y);

cout<<"Length of LCS is "<< lcs( X, Y, m, n ) ;

return 0;
}

// This code is contributed by rathbhupendra