Connection Status:
Competition Arena > MiddleCode
SRM 603 · 2013-12-22 · by subscriber · Brute Force
Class Name: MiddleCode
Return Type: String
Method Name: encode
Arg Types: (string)
Problem Statement

Problem Statement

Hero is learning a new algorithm to encode a string. You are given a String s that consists of lowercase letters only. Your task is to simulate the algorithm described below on this string, so that Hero can see how it works.

The algorithm starts with a given input string s and an empty output string t. The execution of the algorithm consists of multiple steps. In each step, s and t are modified as follows:
  • If the length of s is odd, the middle character of s is added to the end of t, and then deleted from s.
  • If the length of s is even, the two characters in the middle of s are compared. The smaller one of them (either one in case of a tie) is added to the end of t, and then deleted from s.
If after some step the string s is empty, the algorithm terminates. The output of the algorithm is the final string t.

Return the String t that will be produced by the above algorithm for the given String s.

Notes

  • When comparing letters, the smaller one is the one earlier in the alphabet - i.e., the character with the smaller ASCII code.

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • Each character in s will be a lowercase letter ('a'-'z').
Examples
0)
"word"
Returns: "ordw"

In the first step, 'o' is smaller than 'r', thus 'o' is selected. After the first step: s="wrd", t="o". After the second step: s="wd", t="or". In the third step, 'w' is greater than 'd', thus 'd' is selected. After the third step: s="w", t="ord". After the fourth step: s="", t="ordw", and the algorithm terminates.

1)
"aaaaa"
Returns: "aaaaa"
2)
"abacaba"
Returns: "caabbaa"
3)
"shjegr"
Returns: "ejghrs"
4)
"adafaaaadafawafwfasdaa"
Returns: "afawadafawafaafsadadaa"

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

Coding Area

Language: C++17 · define a public class MiddleCode with a public method string encode(string s) · 30 test cases · 2 s / 256 MB per case

Submitting as anonymous