From 73ca25750f543b5da4fb94946124e5069be378e9 Mon Sep 17 00:00:00 2001 From: Harshit Sharma <55287441+harshit-3103@users.noreply.github.com> Date: Thu, 6 Oct 2022 01:11:39 +0530 Subject: [PATCH] Longest Common Subsequence --- .../LongestCommonSubsequence.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Dynamic Programming/LongestCommonSubsequence.cpp diff --git a/Dynamic Programming/LongestCommonSubsequence.cpp b/Dynamic Programming/LongestCommonSubsequence.cpp new file mode 100644 index 0000000..5a8d199 --- /dev/null +++ b/Dynamic Programming/LongestCommonSubsequence.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + + +// take a two string +int lcs(string s, string r){ + int n = s.size(); + int m = r.size(); + vector>dp (n+1,vector(m+1,0)); + + for(int i=1;i<=n;i++){ + for(int j=1;j<=m;j++){ + if(s[i-1]==r[j-1]){ + dp[i][j] = 1 + dp[i-1][j-1]; + } + else{ + dp[i][j] = max(dp[i-1][j] , dp[i][j-1]); + } + } + } + + return dp[n][m]; +} + + +// Main Function +int main() +{ + // take a two string from the user + string s,r; + cin>>s>>r; +// print the length of the common part subsequence + cout<