QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 512 MB 満点: 100

#274. Repeater / Repeater Machine

統計

Little X has found a repeater that can issue commands to a robot. The robot starts at the root of an infinitely extending complete binary tree. You will input a sequence of commands into the repeater, where each character in the sequence can be:

  • L: Commands the robot to move to the left child of the current node.
  • R: Commands the robot to move to the right child of the current node.
  • U: Commands the robot to move to the parent of the current node (if it does not exist, the command is invalid).

After the commands are entered, the repeater will repeat them infinitely. For example, if the command is LR, the robot will follow LRLRLRLR... and continue moving.

There is a connected component of $n$ nodes on this complete binary tree, and it is guaranteed that this component includes the root node. Each node in the connected component contains a treasure. If the robot reaches a location with a treasure, it will mine it. If a location has no treasure, the robot can still visit it. The robot can also pass through the same location multiple times.

Clearly, this connected component is itself a binary tree.

Now, someone has given Little X the preorder traversal of this binary tree containing the treasures. Little X needs to find the shortest possible command sequence such that the robot can mine all the treasures.

Input

A single string consisting of characters 0123, representing the preorder traversal of the binary tree containing the treasures.

  • 0: Represents a node with no children.
  • 1: Represents a node with only a left child.
  • 2: Represents a node with only a right child.
  • 3: Represents a node with both a left and a right child.

Output

An integer representing the length of the shortest command sequence.

Examples

Input 1

1313000

Output 1

3

Note 1

One possible shortest command sequence is LRU.

Input 2

333003003300300

Output 2

15

Subtasks

  • Subtask 1 (31 points): $2 \le n \le 10$.
  • Subtask 2 (32 points): $2 \le n \le 200$.
  • Subtask 3 (37 points): No special restrictions.

For $100\%$ of the data, $2 \le n \le 2 \times 10^3$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.