Longest palindromic subsequence
Not to be confused with Longest palindromic substring.
The longest palindromic subsequence problem is the problem of finding the longest subsequence of a string (a subsequence is obtained by deleting some of the characters from a string without reordering the remaining characters) which is also a palindrome. In general, the longest palindromic subsequence is not unique. For example, the string alfalfa has two palindromic subsequences of length 5: alala and afafa. However, it does not have any palindromic subsequences longer than five characters. Therefore alala and afafa are both considred longest palindromic subsequences of alfalfa.
Precise statement
Three variations of this problem may be distinguished:
- Find the maximum possible length for a palindromic subsequence.
- Find some palindromic subsequence of maximal length.
- Find all longest palindromic subsequences.
Theorem: Returning all longest palindromic subsequences cannot be accomplished in worst-case polynomial time.
Proof[1]: Consider a string made up of ones, followed by zeroes, and finally ones. (Assume is a multiple of 4, although it does not really matter.) Any palindromic substring either does not contain any zeroes, in which case its length is only up to , or it contains at least one zero. If it contains at least one zero, it must be of the form , but and must be equal. (This is because the middle of the palindrome must lie somewhere within the zeroes, otherwise there would be no zeroes on one side of it and at least one zero on the other side; but as long as the middle lies within the zeroes, there must be an equal number of ones on each side.) But can only be up to , and likewise with , so again the palindrome cannot be longer than characters. However, there are palindromic substrings of length ; we can either take all the ones, or we can take all zeroes, all terminal ones, and out of the initial ones. Thus the output size is not polynomial in , and then neither can the algorithm be in the worst case.
However, this does not rule out the existence of a polynomial-time algorithm for the first two variations on the problem. We now present such an algorithm.
Algorithm
todo
References
- ↑ Jonathan T. Schneider (2010). Personal communication.
External links
- IOI '00 - Palindrome
- SPOJ:
- Palindrome 2000 (a duplicate of the problem above)
- Aibohphobia