Possible Duplicate:
how to find longest palindromic subsequence?
Longest palindrome subsequence.
A palinadrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palindromes are all strings of length 1, civic, racecar, and aibohphobia(fear of palindromes).
Give an efficent algorithm to find the longest palindrome that is a subsequence of a given input string. For example, given the input "character", your algorithm should return "caac".
Now, I know how to get the length of the result. How can I get the sequence of the result ?
def mylongest(self, i, j):
if j - i <= 1:
return j-i
else:
if self.sequence[i] == self.sequence[j]:
return self.mylongest(i+1,j-1) + 2
else:
return max (self.mylongest(i+1, j), self.mylongest(i, j-1))