Wesley has recently visited the arcade and has tasked himself with mastering all songs on Dance Dance Revolution. Unfortunately, the machine that Wesley has claimed is broken and is unable to keep track of his score. Can you help Wesley determine his final score?
In Dance Dance Revolution, there are 4 different moves that can be played - up, down, left, and right.
The song that Wesley plays consists of multiple beats and he will match each beat with a single move. Every move is worth U
, D
, L
, and R
, representing the moves up, down, left, and right. We will call this sequence
A combo is defined by a specific ordering of moves played consecutively. There are U
, D
, L
, and R
the
To determine which moves are part of combos, the following algorithm is used.
- Set the current move to the first move that Wesley performs.
- Select the longest combo such that the
moves in the combo match the next moves (starting from the current move) in the sequence of moves that Wesley performs (in the same order), where is the length of the chosen combo. It may be the case that such a combo does not exist, in which case, the longest combo has a length of . - The current move becomes the move immediately after the last move in the longest combo (or the move immediately after the current move if no such combo exists). If the current move is past the end of the sequence, terminate the algorithm.
- Go to step 2.
It can be seen that a single move may only correspond to at most one combo, although the same combo may be played multiple times.
No two combos will be identical.
Given a series of moves that Wesley performs, can you determine how many points he scored?
Constraints
For this problem, you will NOT be required to pass all the samples in order to receive points. In addition, you must pass all previous subtasks to earn points for a specific subtask.
For all subtasks:
U
, D
, L
, and R
.
Subtask 1 [30%]
Subtask 2 [70%]
No additional constraints.
Input Specification
The first line will contain a string U
, D
, L
, or R
.
The second line of input will contain
The next U
, D
, L
, or R
.
Output Specification
Output a single integer, the number of points that Wesley scored.
Sample Input 1
RLUDD
1
RLU 4
Sample Output 1
9
Sample Explanation 1
The first
Sample Input 2
UDULRL
2
ULR 3
UDU 2
Sample Output 2
8
Sample Explanation 2
The first
Sample Input 3
ULRURURU
3
LRURU 500
LRUR 750
URU 1000
Sample Output 3
508
Sample Explanation 3
The second move to the sixth move are all part of the first combo, resulting in
Comments