Connection Status:
Competition Arena > ShortPalindromes
SRM 165 · 2003-09-23 · by vorthys · Dynamic Programming, Recursion
Class Name: ShortPalindromes
Return Type: String
Method Name: shortest
Arg Types: (string)
Problem Statement

Problem Statement

A palindrome is a String that is spelled the same forward and backwards. Given a String base that may or may not be a palindrome, we can always force base to be a palindrome by adding letters to it. For example, given the word "RACE", we could add the letters "CAR" to its back to get "RACECAR" (quotes for clarity only). However, we are not restricted to adding letters at the back. For example, we could also add the letters "ECA" to the front to get "ECARACE". In fact, we can add letters anywhere in the word, so we could also get "ERCACRE" by adding an 'E' at the beginning, a 'C' after the 'R', and another 'R' before the final 'E'. Your task is to make base into a palindrome by adding as few letters as possible and return the resulting String. When there is more than one palindrome of minimal length that can be made, return the lexicographically earliest (that is, the one that occurs first in alphabetical order).

Constraints

  • base contains between 1 and 25 characters, inclusive.
  • Every character in base is an uppercase letter ('A'-'Z').
Examples
0)
"RACE"
Returns: "ECARACE"

To make "RACE" into a palindrome, we must add at least three letters. However, there are eight ways to do this by adding exactly three letters: "ECARACE" "ECRARCE" "ERACARE" "ERCACRE" "RACECAR" "RAECEAR" "REACAER" "RECACER" Of these alternatives, "ECARACE" is the lexicographically earliest.

1)
"TOPCODER"
Returns: "REDTOCPCOTDER"
2)
"Q"
Returns: "Q"
3)
"MADAMIMADAM"
Returns: "MADAMIMADAM"
4)
"ALRCAGOEUAOEURGCOEUOOIGFA"
Returns: "AFLRCAGIOEOUAEOCEGRURGECOEAUOEOIGACRLFA"

Submissions are judged against all 54 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class ShortPalindromes with a public method string shortest(string base) · 54 test cases · 2 s / 256 MB per case

Submitting as anonymous