ABC 243 D - Moves on Binary Tree
提出
code: python
n, x = map(int,input().split())
s = input()
# ライブラリがある...?
# 完全二分木は固定だから...
解答
code: python
n, x = map(int,input().split())
s = input()
t = []
# L または R の直後に U がある場合、この2回の移動で元の頂点に戻るため、このような移動は無視してもよい
for c in s:
if c == "U" and len(t) > 0 and (t-1 == "L" or t-1 == "R"): t.pop()
else:
t.append(c)
for c in t:
if c == "U": x = x//2
if c == "L": x = x*2
if c == "R": x = x*2+1
print(x)
テーマ
メモ