QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#274. 复读 / 复读机

Statistics

小 X 捡到了一台复读机,这台复读机可以向机器人发号施令。机器人将站在一棵完全二叉树的根上,完全二叉树是无限延伸的。你将向复读机录入一串指令,这串指令单个字符可以是:

  • L:命令机器人向当前节点的左子走;
  • R:命令机器人向当前节点的右子走;
  • U:命令机器人向当前节点的父亲走(若没有,则命令非法)。

录入指令后,复读机将会把指令无限复读下去。比如命令为 LR,那么机器人会遵从 LRLRLRLR... 一直走下去。

这棵完全二叉树上有一个 $n$ 个节点的连通块,保证这个连通块包含根节点。连通块上的每个节点都埋有宝藏,机器人到达过的地方如果有宝藏,则会将其开采。如果一个地方没有宝藏,机器人也可以到那里去。机器人也可以反复经过一个地方。

显然,这个连通块本身也是一棵二叉树。

现在,有人告诉了小 X 埋有宝藏的这棵二叉树的前序遍历,小 X 需要寻找到一条尽量短的指令,使得机器人能够挖掘出所有宝藏。

输入格式

一行一个字符串,由 0123 中的字符组成,表示埋有宝藏的这棵二叉树的前序遍历。

  • 0:表示这是一个没有儿子的节点。
  • 1:表示这是一个只有左子的节点。
  • 2:表示这是一个只有右子的节点。
  • 3:表示这是一个既有左子又有右子的节点。

输出格式

一个整数,表示最短指令的长度。

样例数据

样例 1 输入

1313000

样例 1 输出

3

样例 1 解释

一种可行的最短指令为 LRU

样例 2 输入

333003003300300

样例 2 输出

15

子任务

  • Subtask 1(31 points):$2 \le n \le 10$。
  • Subtask 2(32 points):$2 \le n \le 200$。
  • Subtask 3(37 points):无特殊限制。

对于 $100\%$ 的数据,$2 \le n \le 2 \times 10^3$。

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.