Dragon Pattern
Time limit5sMemory limit128 MB
Count how many times pattern S appears as a contiguous block in the length 2^n direction string of the order-n left dragon curve.
- Level
Hard9 of 10
- Topics
- String matching, Recursion, Divide and conquer, Dynamic programming
- Solved
- No attempts yet
Problem
The dragon curve is a polygonal chain built from segments of length 1, defined recursively. Choose a point in the plane and one of the four directions parallel to the coordinate axes. The left (right) dragon of order is drawn like this:
- if is 0, draw a segment of length 1 from the current point in the current direction and move to its endpoint;
- otherwise draw the left dragon of order from the current point in the current direction, turn 90 degrees to the left (right) at the endpoint, then draw the right dragon of order .
Draw the left dragon of order starting at the origin in the positive direction of the axis . It makes moves of length 1 one after another, so writing down the direction of each move in order gives a string of length .
A pattern is also a string of move directions. Count the substrings of that direction string that equal the pattern, that is, the number of positions where the pattern starts.
Input
One line contains an integer and a string describing the pattern, separated by one space. consists only of the letters R, L, U, D. R means a move to the right (positive direction of the axis ), L a move to the left (negative direction of ), U a move up (positive direction of the axis ), and D a move down (negative direction of ).
and .
Output
Print how many times the pattern occurs in the left dragon of order drawn in the positive direction of the axis , modulo .