From 188b068b1dfb75d69019b59a852a5c197564b50e Mon Sep 17 00:00:00 2001 From: Anshika-G Date: Tue, 2 Oct 2018 19:35:05 +0530 Subject: [PATCH] Added lcs --- .../c++/longest_common_subsequence.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 dynamic_programing/longest_common_subsequence/c++/longest_common_subsequence.cpp diff --git a/dynamic_programing/longest_common_subsequence/c++/longest_common_subsequence.cpp b/dynamic_programing/longest_common_subsequence/c++/longest_common_subsequence.cpp new file mode 100644 index 0000000..7b49e04 --- /dev/null +++ b/dynamic_programing/longest_common_subsequence/c++/longest_common_subsequence.cpp @@ -0,0 +1,40 @@ +#include +#include +using namespace std; + +int lpsH(string s, int start, int end, int **output){ + if(start > end){ + return 0; + } + + if(s.length() == 1 || start == end){ + return 1; + } + + if(output[start][end] > -1){ + return output[start][end]; + } + if(s[start] == s[end]){ + output[start][end] = 2 + lpsH(s, start+1, end-1, output); + return output[start][end]; + } + output[start][end] = max(lpsH(s, start+1, end, output), lpsH(s, start, end-1, output)); + return output[start][end]; +} +int lcs(string s1, string s2){ + int n1 = s1.length(); + int **output = new int*[n1+1]; + + for(int i = 0; i <= s1.length(); i++){ + output[i] = new int[s2.length()+1]; + for(int j = 0; j <= s2.length(); j++){ + output[i][j] = -1; + } + } + return lpsH(s1, 0, s1.length()-1, output); +} +int main{ + string s1, s2; + cin >> s1 >> s2; + cout << lcs(s1, s2); +} \ No newline at end of file