PalindromeDecoding
SRM 324 · 2006-10-25 · by AdrianKuegel
SRM 324 · 2006-10-25 · by AdrianKuegel · String Manipulation
Problem Statement
Problem Statement
You are given a String code and int[] s position and length. code contains an encoded string which you must decode using the following method. Step through the elements of position in order, and for each element i, take the substring of length length[i] at position position[i]. Insert the reverse of that substring before position position[i]+length[i], thereby creating a palindromic substring. All positions are 0-based. Return the decoded String .
Constraints
- code will contain between 1 and 10 characters, inclusive.
- code will contain only digits ('0'-'9') and letters ('a'-'z', 'A'-'Z').
- position will contain between 0 and 10 elements, inclusive.
- length will contain the same number of elements as position.
- position and length will always refer to a valid substring in the (partially decoded) string code.
- Each element of length will be positive.
- The return value will have at most 1000 characters.
Examples
0)
"ab"
{0}
{2}
Returns: "abba"
The decoding step selects the whole string and appends it in reversed form.
1)
"Misip"
{2,3,1,7}
{1,1,2,2}
Returns: "Mississippi"
The decoding steps are: "Misip" -> "Missip" -> "Misssip" -> "Mississip" -> "Mississippi"
2)
"XY"
{0, 0, 0, 0}
{2, 4, 8, 16}
Returns: "XYYXXYYXXYYXXYYXXYYXXYYXXYYXXYYX"
In this example the length of the string doubles in each decoding step.
3)
"TC206"
{1,2,5}
{1,1,1}
Returns: "TCCC2006"
4)
"nodecoding"
{}
{}
Returns: "nodecoding"
5)
"0123456789"
{0,0,0,0,0,0,0,0,0,0}
{10,20,40,80,160,320,357,1,1,1}
Returns: "0000123456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210012345678998765433456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210012345678998765432102100123456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210012345678998765432100123456789987654321001234567899876543210"
length 1000
Submissions are judged against all 43 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class PalindromeDecoding with a public method string decode(string code, vector<int> position, vector<int> length) · 43 test cases · 2 s / 256 MB per case