PalindromeEncoding
SRM 324 · 2006-10-25 · by AdrianKuegel
Problem Statement
The following algorithm can be used to encode a sequence of characters s:
- Find any palindrome of even length in s. A palindrome is a string that reads the same forward and backward. If there are no palindromes of even length, go to step 3.
- Remove the last half of the selected palindrome from s. For example, if the palindrome is "0110", remove the "10". Go to step 1.
- Print the remaining sequence of characters.
Note that the resulting sequence is not necessarily unique since there may be multiple palindromes to choose from in step 1.
Given a
Constraints
- s will contain between 1 and 50 characters, inclusive.
- s will contain only the digits '0' and '1'.
"0111001" Returns: 2
First, take the last four digits and remove the "01" to get "01110". Then, select either of the "11"s and remove a "1" to get "0110". Finally, since the entire string is now a palindrome, you can remove the "10" to get "01".
"10000011110101101001000001100011110001100011001110" Returns: 2
"00000000000000000000000000000000000000000000000000" Returns: 1
"11111111111111111111111111111111111111111111111111" Returns: 1
"0" Returns: 1
There is no palindrome of even length in this string, so nothing is changed.
Submissions are judged against all 120 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class PalindromeEncoding with a public method int getLength(string s) · 120 test cases · 2 s / 256 MB per case