Restricted RPS (Codeforces 1245B)
Tags: Greedy, constructive algorithms

Let $$$n$$$ be a positive integer. Let $$$a, b, c$$$ be nonnegative integers such that $$$a + b + c = n$$$.Alice and Bob are gonna play rock-paper-scissors $$$n$$$ times. Alice knows the sequences of hands that Bob will play. However, Alice has to play rock $$$a$$$ times, paper $$$b$$$ times, and scissors $$$c$$$ times.Alice wins if she beats Bob in at least $$$\lceil \frac{n}{2} \rceil$$$ ($$$\frac{n}{2}$$$ rounded up to the nearest integer) hands, otherwise Alice loses.Note that in rock-paper-scissors:  rock beats scissors;  paper beats rock;  scissors beat paper. The task is, given the sequence of hands that Bob will play, and the numbers $$$a, b, c$$$, determine whether or not Alice can win. And if so, find any possible sequence of hands that Alice can use to win.If there are multiple answers, print any of them.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases.Then, $$$t$$$ testcases follow, each consisting of three lines:   The first line contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$).  The second line contains three integers, $$$a, b, c$$$ ($$$0 \le a, b, c \le n$$$). It is guaranteed that $$$a + b + c = n$$$.  The third line contains a string $$$s$$$ of length $$$n$$$. $$$s$$$ is made up of only 'R', 'P', and 'S'. The $$$i$$$-th character is 'R' if for his $$$i$$$-th Bob plays rock, 'P' if paper, and 'S' if scissors. 

Output: For each testcase:   If Alice cannot win, print "NO" (without the quotes).  Otherwise, print "YES" (without the quotes). Also, print a string $$$t$$$ of length $$$n$$$ made up of only 'R', 'P', and 'S' — a sequence of hands that Alice can use to win. $$$t$$$ must contain exactly $$$a$$$ 'R's, $$$b$$$ 'P's, and $$$c$$$ 'S's.  If there are multiple answers, print any of them. The "YES" / "NO" part of the output is case-insensitive (i.e. "yEs", "no" or "YEs" are all valid answers). Note that 'R', 'P' and 'S' are case-sensitive.

Note: In the first testcase, in the first hand, Alice plays paper and Bob plays rock, so Alice beats Bob. In the second hand, Alice plays scissors and Bob plays paper, so Alice beats Bob. In the third hand, Alice plays rock and Bob plays scissors, so Alice beats Bob. Alice beat Bob 3 times, and $$$3 \ge \lceil \frac{3}{2} \rceil = 2$$$, so Alice wins.In the second testcase, the only sequence of hands that Alice can play is "RRR". Alice beats Bob only in the last hand, so Alice can't win. $$$1 < \lceil \frac{3}{2} \rceil = 2$$$.

Sample Inputs:
2
3
1 1 1
RPS
3
3 0 0
RPS

Sample Outputs:
YES
PSR
NO

Solution (Python):
import math
for t in range(int(input())):
    n = int(input())
    (a, b, c) = map(int, input().split())
    s = input()
    x = ''
    for i in s:
        if i == 'R':
            if b > 0:
                b -= 1
                x += 'P'
            else:
                x += 'A'
        if i == 'P':
            if c > 0:
                c -= 1
                x += 'S'
            else:
                x += 'A'
        if i == 'S':
            if a > 0:
                a -= 1
                x += 'R'
            else:
                x += 'A'
    if len(x) - x.count('A') >= math.ceil(n / 2):
        print('YES')
        y = ''
        for i in x:
            if i != 'A':
                y += i
            elif a > 0:
                a -= 1
                y += 'R'
            elif b > 0:
                b -= 1
                y += 'P'
            else:
                c -= 1
                y += 'S'
        print(y)
    else:
        print('NO')

Solution (Python):
from math import ceil
T = int(input())
for t in range(T):
    N = int(input())
    (R, P, S) = map(int, input().split())
    bob = list(input().strip())
    ans = [None] * N
    win = 0
    for n in range(N):
        if bob[n] == 'R' and P > 0:
            ans[n] = 'P'
            P -= 1
            win += 1
        if bob[n] == 'P' and S > 0:
            ans[n] = 'S'
            S -= 1
            win += 1
        if bob[n] == 'S' and R > 0:
            ans[n] = 'R'
            R -= 1
            win += 1
    remain = list('R' * R + 'P' * P + 'S' * S)
    for n in range(N):
        if ans[n] == None:
            ans[n] = remain.pop()
    if win >= ceil(N / 2):
        print('YES')
        print(''.join(ans))
    else:
        print('NO')

Solution (Python):
from random import randint

def fin_st(l_i, st, l, rules):
    dict_a = {'R': l[0], 'P': l[1], 'S': l[2]}
    fin_str = ''
    for i in st:
        if dict_a[rules[i]] > 0:
            fin_str += rules[i]
            dict_a[rules[i]] -= 1
        else:
            t = randint(0, len(l_i) - 1)
            fin_str += l_i[t]
            del l_i[t]
    return fin_str
for i in range(int(input())):
    n = int(input())
    (a, b, c) = map(int, input().split())
    st = input()
    rules = {'S': 'R', 'P': 'S', 'R': 'P'}
    dict_a = {'R': a, 'P': b, 'S': c}
    dict_b = {'R': 0, 'P': 0, 'S': 0}
    for i in st:
        dict_b[i] += 1
    wins = 0
    for i in dict_b:
        if dict_a[rules[i]] >= dict_b[i]:
            wins += dict_b[i]
            dict_a[rules[i]] -= dict_b[i]
        else:
            wins += dict_a[rules[i]]
            dict_a[rules[i]] = 0
    if round(n / 2 + 1e-09) <= wins:
        print('YES')
        print(fin_st(['R'] * dict_a['R'] + ['P'] * dict_a['P'] + ['S'] * dict_a['S'], st, [a, b, c], rules))
    else:
        print('NO')