COP 3503 Recitation #7 Problem Dynamic Programming – Longest Palindrome

advertisement
COP 3503 Recitation #7 Problem
Dynamic Programming – Longest Palindrome
Problem: Longest Palindrome
In class, we went over a dynamic programming solution to the Longest Common
Subsequence problem. Now you want to find the longest palindrome that is a
subsequence of a given string. (Remember that a subsequence of a string S, is a string
created by deleting zero or more characters from S. In other words, the letters of a
subsequence of S appear in order in S, but they are not required to be contiguous.) For
example, the longest palindrome that is a subsequence of the string
“I_AMA_RACECAR_DRIVER” is “I_RACECAR_I”. In your solution, you may
assume that all the input strings can contain any alphanumeric characters or symbols, but
no spaces. Here is the signature your method must have:
public static String longestPal(String str1);
In order to simplify the grading of the assignment, after you create your method, write a
main program to read from a file called palindromes.in, process several input cases, and
write out to the screen.
Input File Format
The first line of the input file will contain a single positive integer n, representing the
number of test cases in the file. The n test cases will follow. Each test case will contain a
single string.
Output Format
For each test case, print out a single line with the format:
Test case k: X
where X is the string that contains the subsequence that is longest palindrome that you
could create with the input string. Note: 1 ≤ k ≤ n.
Sample Input
3
SARAH
BO
I_AMA_RACECAR_DRIVER
Sample Output
Test case 1: ARA
Test case 2: B
Test case 3: I_RACECAR_I
(NOTE: In the case that there is not a palindrome longer than 1 character, return the 1st
character, as in Test Case #2 above.)
Please turn in your LongestPalindrome.java containing both your main as well as your
longestPal function.
Download