Palindromist
SRM 294 · 2006-03-25 · by legakis
Problem Statement
A palindrome is a phrase that reads the same forward and backward (ignoring spaces). Given the first half of a palindrome (as described below), you must return a complete palindrome that contains only words from a given set of legal words. The returned palindrome must be a phrase where words are separated by single spaces.
You will be given the first half of the palindrome as a
For example, given the list of words { "A", "CANAL", "MAN", "PANAMA", "PLAN" }, and the text "AMANAPLANAC", your method would return the
If no palindrome can be made, your method should return "". If more than one palindrome can be made, return the one that comes first lexicographically (please note that ' ' comes before all letters).
Constraints
- text will contain between 1 and 50 characters, inclusive.
- text will contain only uppercase letters ('A'-'Z').
- words will contain between 1 and 50 elements, inclusive.
- Each element of words will contain between 1 and 50 characters, inclusive.
- Each element of words will contain only uppercase letters ('A'-'Z').
"AMANAPLANAC"
{ "A", "CANAL", "MAN", "PANAMA", "PLAN" }
Returns: "A MAN A PLAN A CANAL PANAMA"
"AAAAA"
{ "AA", "A", "AAA" }
Returns: "A A A A A A A A A"
"CBA"
{ "CBABC", "CBAABC" }
Returns: "CBAABC"
"RACEFAST"
{ "AR", "CAR", "FAST", "RACE", "SAFE", "CEFA", "ACE", "STTS", "AFEC" }
Returns: "RACE FAST SAFE CAR"
"AABAABA"
{ "AA", "AAB", "BAA", "BAB" }
Returns: "AA BAA BAA BAA BAA"
Submissions are judged against all 137 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Palindromist with a public method string palindrome(string text, vector<string> words) · 137 test cases · 2 s / 256 MB per case