#abc243d. D - Moves on Binary Tree
D - Moves on Binary Tree
Score : points
问题描述
存在一棵完全二叉树,拥有 个顶点,编号分别为 。
顶点 是根节点。对于每个 ,顶点 拥有两个子节点:左边为顶点 ,右边为顶点 。
高桥从顶点 出发,并执行由字符串 表示的 步操作。第 步操作如下:
- 若 的第 个字符是
U
,则前往当前所在顶点的父节点。 - 若 的第 个字符是
L
,则前往当前所在顶点的左子节点。 - 若 的第 个字符是
R
,则前往当前所在顶点的右子节点。
请计算高桥在执行完 步操作后所在的顶点的编号。在给定的情况下,保证答案不超过 。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There is a perfect binary tree with vertices, numbered .
Vertex is the root. For each , Vertex has two children: Vertex to the left and Vertex to the right.
Takahashi starts at Vertex and performs moves, represented by a string . The -th move is as follows.
- If the -th character of is
U
, go to the parent of the vertex he is on now. - If the -th character of is
L
, go to the left child of the vertex he is on now. - If the -th character of is
R
, go to the right child of the vertex he is on now.
Find the index of the vertex Takahashi will be on after moves. In the given cases, it is guaranteed that the answer is at most .
Constraints
- and are integers.
- has a length of and consists of
U
,L
, andR
. - When Takahashi is at the root, he never attempts to go to the parent.
- When Takahashi is at a leaf, he never attempts to go to a child.
- The index of the vertex Takahashi is on after moves is at most .
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
3 2
URL
Sample Output 1
6
The perfect binary tree has the following structure.
In the three moves, Takahashi goes .
Sample Input 2
4 500000000000000000
RRUU
Sample Output 2
500000000000000000
During the process, Takahashi may be at a vertex whose index exceeds .
Sample Input 3
30 123456789
LRULURLURLULULRURRLRULRRRUURRU
Sample Output 3
126419752371
update @ 2024/3/10 10:27:24