An ant walks the left/right perimeter of a tree; record U/D at each fork. Index kkk is the fork after kkk steps from the root. Output the tree distance between indices AAA and BBB.
U
D
Line 1: AAA, BBB. Line 2: UD string SSS.
One integer distance.
∣S∣<106|S| < 10^6∣S∣<106; SSS encodes a valid tree.