fix: segv in longest_palindromic_subsequence.cpp (#2461)

* fix: initialise properly res, set properly size of ans

* test: add check with empty input

* style: use const reference as input type

* refactor: add ind_type

* style: use reverse interators to b

* style: use auto in definition of idx

* updating DIRECTORY.md

* style: clean-up includes

* style: use std::string::size_type in definition of ind_type

---------

Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
Piotr Idzik 2023-05-18 19:26:06 +02:00 committed by GitHub
parent 6027480643
commit 4fc14710b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,26 +13,26 @@
* @author [Anjali Jha](https://github.com/anjali1903) * @author [Anjali Jha](https://github.com/anjali1903)
*/ */
#include <algorithm> #include <cassert> /// for assert
#include <cassert> #include <string> /// for std::string
#include <iostream> #include <vector> /// for std::vector
#include <vector>
/** /**
* Function that returns the longest palindromic * Function that returns the longest palindromic
* subsequence of a string * subsequence of a string
*/ */
std::string lps(std::string a) { std::string lps(const std::string& a) {
std::string b = a; const auto b = std::string(a.rbegin(), a.rend());
reverse(b.begin(), b.end()); const auto m = a.length();
int m = a.length(); using ind_type = std::string::size_type;
std::vector<std::vector<int> > res(m + 1); std::vector<std::vector<ind_type> > res(m + 1,
std::vector<ind_type>(m + 1));
// Finding the length of the longest // Finding the length of the longest
// palindromic subsequence and storing // palindromic subsequence and storing
// in a 2D array in bottoms-up manner // in a 2D array in bottoms-up manner
for (int i = 0; i <= m; i++) { for (ind_type i = 0; i <= m; i++) {
for (int j = 0; j <= m; j++) { for (ind_type j = 0; j <= m; j++) {
if (i == 0 || j == 0) { if (i == 0 || j == 0) {
res[i][j] = 0; res[i][j] = 0;
} else if (a[i - 1] == b[j - 1]) { } else if (a[i - 1] == b[j - 1]) {
@ -43,10 +43,10 @@ std::string lps(std::string a) {
} }
} }
// Length of longest palindromic subsequence // Length of longest palindromic subsequence
int idx = res[m][m]; auto idx = res[m][m];
// Creating string of index+1 length // Creating string of index+1 length
std::string ans(idx + 1, '\0'); std::string ans(idx, '\0');
int i = m, j = m; ind_type i = m, j = m;
// starting from right-most bottom-most corner // starting from right-most bottom-most corner
// and storing them one by one in ans // and storing them one by one in ans
@ -73,12 +73,10 @@ std::string lps(std::string a) {
/** Test function */ /** Test function */
void test() { void test() {
// lps("radar") return "radar"
assert(lps("radar") == "radar"); assert(lps("radar") == "radar");
// lps("abbcbaa") return "abcba"
assert(lps("abbcbaa") == "abcba"); assert(lps("abbcbaa") == "abcba");
// lps("bbbab") return "bbbb"
assert(lps("bbbab") == "bbbb"); assert(lps("bbbab") == "bbbb");
assert(lps("") == "");
} }
/** /**