From d5249ddc9b4fec3e697c58a01c7519f26338a107 Mon Sep 17 00:00:00 2001 From: Komal2531 Date: Fri, 10 Sep 2021 13:30:08 +0530 Subject: [PATCH] C++ LCS algorithm added --- c++/Longest_Common_Subsequence.cpp | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 c++/Longest_Common_Subsequence.cpp diff --git a/c++/Longest_Common_Subsequence.cpp b/c++/Longest_Common_Subsequence.cpp new file mode 100644 index 0000000..8946ae7 --- /dev/null +++ b/c++/Longest_Common_Subsequence.cpp @@ -0,0 +1,40 @@ +/*Longest Common Subsequence Algorithm*/ + +#include +using namespace std; + +#define ll long long +#define int long long +#define pb push_back +#define endl "\n" +#define F first +#define S second +#define popcount(x) __builtin_popcountll(x) +typedef unsigned long long ull; + + +const int M=1000000007; +const int MM=998244353; +const int INF =9e18; + +int lcs(string a,string b,int m,int n) +{ + if(m==0||n==0) + return 0; + if(a[m-1]==b[n-1]) + return (1LL+lcs(a,b,m-1,n-1)); + int x=lcs(a,b,m,n-1); + int y=lcs(a,b,m-1,n); + return max(x,y); + +} +signed main() +{ + string a,b; + cin>>a>>b; + int m=a.size(),n=b.size(); + int ans=lcs(a,b,m,n); + cout<<"Length of Longest common subsequence of "<