From edd2193739fc1581a3ced74d981b25daf8064963 Mon Sep 17 00:00:00 2001 From: mitali004 <33895765+mitali004@users.noreply.github.com> Date: Sat, 16 Dec 2017 10:24:41 +0530 Subject: [PATCH 1/5] Create lps.c --- .../Longest Palindromic Sub-sequence/lps.c | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c diff --git a/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c b/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c new file mode 100644 index 000000000..6cdee63c6 --- /dev/null +++ b/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c @@ -0,0 +1,48 @@ +#include +#include +#define MAXLEN 100 +// A utility function to get max of two integers +int max (int x, int y) { return (x > y)? x : y; } + +// Returns the length of the longest palindromic subsequence in seq +int lps(char *str) +{ + int n = strlen(str); + int i, j, len; + int mat[n][n]; // Create a table to store results of subproblems + + + // Strings of length 1 are palindrome of lentgh 1 + for (i = 0; i < n; i++) + mat[i][i] = 1; + + /* Build the table. Note that the lower diagonal values of table are + useless and not filled in the process. len is length of + substring*/ + for (len=2; len<=n; len++) + { + for (i=0; i Date: Sat, 16 Dec 2017 10:51:11 +0530 Subject: [PATCH 2/5] Create readme_lps.md --- .../Longest Palindromic Sub-sequence/readme_lps.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/readme_lps.md diff --git a/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/readme_lps.md b/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/readme_lps.md new file mode 100644 index 000000000..2534f1124 --- /dev/null +++ b/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/readme_lps.md @@ -0,0 +1,13 @@ +## Longest Common Subsequence + In order to solve this problem using Dynamic Programming, we create a 2-D matrix called mat[n][n], where 'n' is the length of the given string. +Any entry [i][j] in the matrix represents the length of the longest palindromic sub-sequence from 'i'th charecter of the string to the 'j'th. +As the string is always considereed from left to right i<=j always. Hence the lower triangle of the matrix is of no use. + +### Filling of the matrix + Every single charecter contributes to a palindromic string of length 1 starting and ending at itself, therefore we fill the diagonal of the +matrix with 1 for every [i][i] entry.

+Let str[0..n-1] be the input sequence of length n, then mat[0][n-1] will be the length of the longest palindromic subsequence.
+For every other entry:
+If last and first characters of str are same, then mat[i][j] = mat[i+1][j-1] + 2, as it considers [2(for the first and the last chaar)] + +[longest palindromic subsequence between these 2 chars].
+Else mat[i][j] = max(mat[i][j-1], mat[i+1][j]). From 1642c7769c5e958b7ed4d01eff797c3943a33ea7 Mon Sep 17 00:00:00 2001 From: mitali004 <33895765+mitali004@users.noreply.github.com> Date: Sat, 16 Dec 2017 11:04:47 +0530 Subject: [PATCH 3/5] Update lps.c --- .../Dynamic Programming/Longest Palindromic Sub-sequence/lps.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c b/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c index 6cdee63c6..3a8f4693b 100644 --- a/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c +++ b/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c @@ -41,7 +41,7 @@ int main() { printf("Enter your string\n"); char str[MAXLEN]; - scanf("%s",str); + scanf("100%s",str); printf ("The lnegth of the Longest Palindromic Sub-sequence is %d", lps(str)); getchar(); return 0; From 0b52d94bf8a84170229dfbee4505f62b256691c1 Mon Sep 17 00:00:00 2001 From: mitali004 <33895765+mitali004@users.noreply.github.com> Date: Sat, 16 Dec 2017 11:11:18 +0530 Subject: [PATCH 4/5] Update lps.c --- .../Dynamic Programming/Longest Palindromic Sub-sequence/lps.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c b/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c index 3a8f4693b..9ad4bd223 100644 --- a/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c +++ b/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c @@ -41,7 +41,7 @@ int main() { printf("Enter your string\n"); char str[MAXLEN]; - scanf("100%s",str); + scanf("%100s",str); printf ("The lnegth of the Longest Palindromic Sub-sequence is %d", lps(str)); getchar(); return 0; From 460893461150270a6187e6c412364b3a36356534 Mon Sep 17 00:00:00 2001 From: mitali004 <33895765+mitali004@users.noreply.github.com> Date: Sat, 16 Dec 2017 11:15:22 +0530 Subject: [PATCH 5/5] Update lps.c --- .../Dynamic Programming/Longest Palindromic Sub-sequence/lps.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c b/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c index 9ad4bd223..f2262efb0 100644 --- a/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c +++ b/Competitive Coding/Dynamic Programming/Longest Palindromic Sub-sequence/lps.c @@ -41,7 +41,7 @@ int main() { printf("Enter your string\n"); char str[MAXLEN]; - scanf("%100s",str); + scanf("%99s",str); printf ("The lnegth of the Longest Palindromic Sub-sequence is %d", lps(str)); getchar(); return 0;