P5597 [XR-4] Repeater.

Background

**Contest reminder: When the robot is at the root of this complete binary tree, executing `U` is illegal. That is, you must ensure this can never happen.** **Contest reminder: This binary tree extends infinitely downward, meaning every node has a left child and a right child. All nodes except the root have a parent.**

Description

X found a repeater machine, which can be used to give commands to a robot. The robot will stand at the root of a complete binary tree, and this complete binary tree extends infinitely. You will input a string of commands into the repeater. Each character of the command string can be: * `L`: command the robot to move to the left child of the current node. * `R`: command the robot to move to the right child of the current node. * `U`: command the robot to move to the parent of the current node (if there is no parent, then the command is illegal). After the commands are entered, the repeater will repeat them forever. For example, if the command is `LR`, then the robot will follow `LRLRLRLR...` and keep moving. On this complete binary tree, there is a connected component containing $n$ nodes, and it is guaranteed that this component contains the root. Every node in the component has a treasure buried. If the robot ever reaches a place with a treasure, it will mine it. If a place has no treasure, the robot can still go there. The robot may also pass through the same place multiple times. Clearly, this connected component itself is also a binary tree. Now, someone tells X the preorder traversal of this treasure-burying binary tree. X needs to find a command string as short as possible so that the robot can mine all treasures.

Input Format

One line containing a string consisting of characters from `0123`, representing the preorder traversal of the treasure-burying binary tree. * `0`: a node with no children. * `1`: a node with only a left child. * `2`: a node with only a right child. * `3`: a node with both a left child and a right child.

Output Format

One integer, the length of the shortest command string.

Explanation/Hint

[Sample 1 Explanation] One possible shortest command string is `LRU`. --- **This problem uses bundled testdata.** - Subtask 1 (31 points): $2 \le n \le 10$. - Subtask 2 (32 points): $2 \le n \le 200$. - Subtask 3 (37 points): no special constraints. For $100\%$ of the data, $2 \le n \le 2 \times 10^3$. Translated by ChatGPT 5