Difficulty : Simple
Pre-requisites : basic math
The problem is one of the easiest in the set. The solution is as follows.
When we have an odd-length string S, we can make one turn and set some flag the the answer is even. Otherwise, we should keep in mind than the answer is odd.
The remaining part always has even length, and we can threat the tree as the octary one and numerate the nodes by consecutive natural numbers. When we have found the node number in this tree, we will only need to multiply it by two and possibly to subtract one. The tree is octary, so the consecutive symbols can be grouped in pairs and each pair will denote a single edge of the tree. Namely, in the order from left to right the order of pairs is “ll”, “lr”, “rl”, “rr”.
So we just maintain the variable index and each time we traverse one edge of our tree, we multiply it by 4, and then add -2, -1, 0 or 1, depending on the type of the edge: -2 for “ll”, -1 for “lr” and so on.
At the end we just need to find the index-th even (or odd) number. The parity depends on the parity of the length of the string that denotes the path.
Setter’s solution: link
Tester’s solution: link