Moves on an Infinite Binary Tree
Time limit2sMemory limit128 MB
Starting from the node reached by S, count the distinct nodes reachable by following any subsequence of T on an infinite binary tree.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Tree, String
- Solved
- No attempts yet
Problem
A binary tree is a tree data structure in which every node has at most two children. The two children are usually told apart as the left child and the right child, and a node that has a child is called the parent of that child.
An instruction string is a string made up only of L, R, and U. L means left, R means right, and U means up.
Sanghyun drew an infinite binary tree. Every node of this tree has two children, and every node has a parent. The parent of the root is the root itself. Starting at the root, Sanghyun walks the tree by following the instruction string that Kimsung sent him, one letter at a time. L moves to the left child, R moves to the right child, and U moves to the parent.
Just as he is about to start on , Gangsu sends another instruction string . Sanghyun follows to its end and then immediately follows . Following leaves him worn out, so he may skip some of the letters of . He freely picks how many letters to skip and which ones, and he follows the letters that remain in their original order. Count the nodes where the walk can end.
For example, if is L and is LU, the answer is . Following stops at the left child of the root, and from there can be followed in four ways.
- Skipping both letters of stops at the left child of the root.
- Skipping L stops at the root.
- Skipping U stops at the left child of the left child of the root.
- Skipping nothing stops at the left child of the root.
Two of the four ways stop at the same node, so the number of distinct nodes is .
Input
The first line contains the number of test cases . ()
Each test case takes two lines. The first line holds the instruction string and the second line holds the instruction string . Both strings are made up only of L, R, and U, and neither one is longer than .
Output
For each test case, print one line in the form Case i: x. Here is the test case number starting from , and is the number of distinct nodes where the walk can end. The answer can grow very large, so print modulo .