Decoding the Hallway
Time limit1sMemory limit128 MB
For each query, decide whether the given string appears as a contiguous substring of the turn record built after n hallway walks.
- Level
Medium7 of 10
- Topics
- String, Recursion, Divide and conquer, String matching
- Solved
- No attempts yet
Problem
Edward is 21 now. To renew his State Alchemist title he has to pass an exam, and this year the exam is held in a long hallway. Each alchemist enters at the left end, comes out at the right end, and repeats the walk times.
On every walk the alchemist folds the hallway segments he meets, alternating right, left, right, left in the order he meets them. Folding one segment creates a single turn there and splits that segment in two.
- On the first walk the hallway is one straight segment, so he folds that segment to the right.
- On the second walk there are two segments. He folds the first one to the right and the second one to the left.
- On the third walk there are four segments, folded right, left, right, left in that order.
- The fourth and fifth walks continue the same way.
The -th walk meets segments, so after walks the hallway has turns.
Edward finished without a single mistake. Now a judge checks the folding. The judge also enters at the left end and comes out at the right end, writing down every turn he passes, R for a right turn and L for a left turn. A fold direction is not written down as is, so use these four records as the reference:
- : L
- : LLR
- : LLRLLRR
- : LLRLLRRLLLRRLRR
The record grows exponentially with , which makes checking it by hand hard. The judges therefore prepared some strings in advance and worked out for each one whether it appears in the final record as a substring. Then they lost that answer sheet. Help them get it back.
Input
The first line contains the number of test cases . ()
Each of the next lines contains an integer and a string separated by a space. is the number of times Edward walked the hallway and is the string the judge wants to check. consists only of the letters L and R. (, )
The length of is never greater than the length of the record for .
Output
For each test case print one line, Case x: Yes or Case x: No, where is the test case number starting from 1. Print Yes when appears as a substring of the record of the hallway after walks, and No otherwise.