From 156a0b41d39c1729100f1c36b285d0a81c329d18 Mon Sep 17 00:00:00 2001 From: Aditi Saxena <49093585+geekaditi@users.noreply.github.com> Date: Sat, 10 Oct 2020 03:29:01 +0530 Subject: [PATCH] Create Longest-Common-Subsequence LCS problem solved in CPP using DP --- Languages/C++/Longest-Common-Subsequence | 65 ++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Languages/C++/Longest-Common-Subsequence diff --git a/Languages/C++/Longest-Common-Subsequence b/Languages/C++/Longest-Common-Subsequence new file mode 100644 index 000000000..88b58423b --- /dev/null +++ b/Languages/C++/Longest-Common-Subsequence @@ -0,0 +1,65 @@ +/* Longest Common Subsequence problem solved in CPP using Dynamic Programming */ + +#include +#include +using namespace std; + +int LCS(char *a,char *b){ + int n = strlen(a); + int m = strlen(b); + + int dp[100][100] = {0}; + for(int i=1;i<=n;i++){ + for(int j=1;j<=m;j++){ + if(a[i-1] == b[j-1]){ + dp[i][j] = 1 + dp[i-1][j-1]; + } + else{ + dp[i][j]=max(dp[i-1][j],dp[i][j-1]); + } + } + } + + for(int i=0;i<=n;i++){ + for(int j=0;j<=m;j++){ + cout<=0){ + if(a[i-1] == b[j-1]){ + ans[k] = a[i-1]; + k--;i--;j--; + } + else{ + if(dp[i-1][j]>dp[i][j-1]){ + i--; + } + else{ + j--; + } + } + } + cout<<"LCS is "<