Ravi's Stationery Program II
As the new coordinator of Ravi's Stationery Program (RSP), Zach is changing up the annual Rock Paper Scissors tournament. Each student picks a move, either Rock (R), Scissors (S), or Paper (P) and then they line up in a single row.
Zach then selects a contiguous sequence of students from the line to compete in Rock, Paper, Scissors, following these rules:
- Zach chooses any two students who are standing next to each other to face off. Each student always plays the move they picked at the start.
- The standard rules apply: Rock beats Scissors, Scissors beats Paper, and Paper beats Rock.
- The winner remains in the game, and the loser is eliminated, and leaves the line.
- If the two students choose the same move, the rightmost player is eliminated.
This process continues until only one student remains.
Zach wants to know how many contiguous sequences of students he can choose so that a "Rock" student ends up as the final winner of the tournament. Remember, Zach gets to decide who plays in each round, but the students who play must be next to each other.
Input
The first line of the input contains a single integer, (
), the number of students in RSP.
The second line of the input contains a string of characters, each one of
R
, S
or P
, where:
R
means that student will always play rock.S
means that student will always play scissors.P
means that student will always play paper.
Output
Output the number of contiguous sequences of students Zach can choose, so that a student who always plays Rock (R
) can be the final winner after they face off.
Example
Input 1
7
RRPSRPP
Output 1
14
Zach can select the following sequences of students to compete, so that a "Rock" student wins:
Here's an example of how Rock can win if Zach picks students 2 to 5 (inclusive):
- Student 2 (Paper) fights Student 3 (Scissors). Student 2 loses and is knocked out.
- Student 3 (Scissors) fights Student 4 (Rock). Student 3 loses and is eliminated.
- Student 1 (Rock) fights student 4 (Rock). Student 4 loses and is knocked out.
Student 1 (Rock) is the last player left, and wins the tournament.
Note that this is not the only way for Rock to win, nor is Rock guaranteed to win if the match-ups were chosen differently.
Input 2
4
SRRS
Output 2
8
Zach can select the following sequences of students to compete, so that a "Rock" student wins:
Input 3
5
PRPPS
Output 3
2
Input 4
10
RRRRRRRRRR
Output 4
55
Comments