From 69c85749c28234fbb605ed4c01df142513da42b0 Mon Sep 17 00:00:00 2001 From: Abdurrezzak Efe Date: Mon, 6 Nov 2017 22:26:48 +0200 Subject: [PATCH] Add files via upload --- 14.LCS.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 14.LCS.cpp diff --git a/14.LCS.cpp b/14.LCS.cpp new file mode 100644 index 0000000..cf469e7 --- /dev/null +++ b/14.LCS.cpp @@ -0,0 +1,63 @@ +/* + * This program applies LC algorithm on two string user enters + * + * Coded by: Abdurrezak EFE + * */ + +#include +#include +#include +#include +using namespace std; + +int d[10000][10000]; // dynamic programming table + +string reverser(string str) +{ + string s=""; + for(int i = str.size()-1;i>-1;i--) + s+=str[i]; + return s; +} + +string LCS(string s1, string s2) +{ + for(int i=0;i0 && j>0) //traceback for the string + if(d[i][j] == 1+d[i-1][j-1] && s1[i-1] == s2[j-1]) //if it was a match + str += s1[i-1], i--, j--; + else if(d[i][j-1] == d[i][j]) //if it was an insert + j--; + else if(d[i-1][j] == d[i][j]) //if it was a delete + i--; + + return reverser(str); +} + +int main() +{ + string s1,s2; + + cin >> s1 >> s2; + + cout << "The Longest Common Subsequence of " << s1 << " and " << s2 << " is: " << endl + <