longest_palindromic_subsequence.cpp File Reference

Program to find the Longest Palindormic Subsequence of a string. More...

`#include <algorithm>`
`#include <cassert>`
`#include <iostream>`
`#include <vector>`
Include dependency graph for longest_palindromic_subsequence.cpp:

## Functions

std::string | lps (std::string a)
void | test ()
int | main ()

## Detailed Description

Program to find the Longest Palindormic Subsequence of a string.

Palindrome string sequence of characters which reads the same backward as forward Subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

Author
  Anjali Jha

## Function Documentation

## lps()

std::string lps | ( | std::string | a | ) |

Function that returns the longest palindromic subsequence of a string

{
std::string b = a;
reverse(b.begin(), b.end());
int m = a.length();
std::vector<std::vector<int> > res(m + 1);
// Finding the length of the longest
// palindromic subsequence and storing
// in a 2D array in bottoms-up manner
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= m; j++) {
if (i == 0 || j == 0) {
res[i][j] = 0;
} else if (a[i - 1] == b[j - 1]) {
res[i][j] = res[i - 1][j - 1] + 1;
} else {
res[i][j] = std::max(res[i - 1][j], res[i][j - 1]);
}
}
}
// Length of longest palindromic subsequence
int idx = res[m][m];
// Creating string of index+1 length
std::string ans(idx + 1, '\0');
int i = m, j = m;
// starting from right-most bottom-most corner
// and storing them one by one in ans
while (i > 0 && j > 0) {
// if current characters in a and b are same
// then it is a part of the ans
if (a[i - 1] == b[j - 1]) {
ans[idx - 1] = a[i - 1];
i--;
j--;
idx--;
}
// If they are not same, find the larger of the
// two and move in that direction
else if (res[i - 1][j] > res[i][j - 1]) {
i--;
} else {
j--;
}
}
return ans;
}
std::string
std::string::begin
T begin(T... args)
std::string::end
T end(T... args)
ans
ll ans(ll n)
Definition: matrix_exponentiation.cpp:91
std::max
T max(T... args)
std::reverse
T reverse(T... args)
std::vector
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main Function

{
test(); // execute the tests
return 0;
}
test
void test()
Definition: longest_palindromic_subsequence.cpp:75
Here is the call graph for this function:

## test()

void test | ( |  | ) |

Test function

{
// lps("radar") return "radar"
assert(lps("radar") == "radar");
// lps("abbcbaa") return "abcba"
assert(lps("abbcbaa") == "abcba");
// lps("bbbab") return "bbbb"
assert(lps("bbbab") == "bbbb");
}
lps
std::string lps(std::string a)
Definition: longest_palindromic_subsequence.cpp:25
Here is the call graph for this function: