Editorial for TLE '17 Contest 2 P1 - Cadadr


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: ZQFMGB12

This problem is relatively simple and can be accomplished in several ways.

One way is to iterate through the input string and for each a, output (car and for each d, output (cdr.

After we go through the string, we output x and all of the required ). In particular, we need to output ) for each a or d in the original string. Additionally, make sure there is a space between the last car or cdr and x.

Another method involves adding to an answer string and outputting the string at the end, but one must be cautious since in certain languages, adding strings takes \mathcal{O}(|S|) time, where S is the answer string. If we add a string to an answer string each time, we might have a running time of \mathcal{O}(|S|^2), which only earns 50\% of the points.

Time Complexity: \mathcal{O}(|S|)


Comments

There are no comments at the moment.