Explanation:
To win the game, Chef needs to score more than ⌊N/2⌋ (i.e. W = N/2+1) points. It doesn’t matter if Chef loses or draws D = N - (N/2+1) rounds.
Among the characters ‘R’, ‘P’ and ‘S’, ‘P’ is lexicographically smallest.
To find the lexicographically smallest sequence of moves, Chef will play ‘P’ move D times from the beginning of the game. Each time when Chef loses or draws playing move ‘P’, D will be decreased by 1. When D becomes 0 proceeding in this fashion, then Chef will play the winning moves (i.e. ‘R’ against ‘S’, ‘P’ against ‘R’ and ‘S’ against ‘P’).
Code:
#include <stdio.h>
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
char s[n + 1];
scanf("%s", s);
char ans[n + 1];
for (int i = 0; i < n; i++) {
if (s[i] == 'R') {
ans[i] = 'P';
} else if (s[i] == 'P') {
ans[i] = 'S';
} else {
ans[i] = 'R';
}
}
ans[n] = '\0';
int rep = (n - 1) / 2;
for (int i = 0; i < n; i++) {
if (rep > 0 && ans[i] != 'P') {
rep--;
ans[i] = 'P';
}
}
printf("%s\n", ans);
}
return 0;
}
Hope it helps!