Mocha and Red and Blue (Codeforces 1559B)
Tags: Greedy, dp

As their story unravels, a timeless tale is told once again...Shirahime, a friend of Mocha's, is keen on playing the music game Arcaea and sharing Mocha interesting puzzles to solve. This day, Shirahime comes up with a new simple puzzle and wants Mocha to solve them. However, these puzzles are too easy for Mocha to solve, so she wants you to solve them and tell her the answers. The puzzles are described as follow.There are $$$n$$$ squares arranged in a row, and each of them can be painted either red or blue.Among these squares, some of them have been painted already, and the others are blank. You can decide which color to paint on each blank square.Some pairs of adjacent squares may have the same color, which is imperfect. We define the imperfectness as the number of pairs of adjacent squares that share the same color.For example, the imperfectness of "BRRRBBR" is $$$3$$$, with "BB" occurred once and "RR" occurred twice.Your goal is to minimize the imperfectness and print out the colors of the squares after painting. 

Input: Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of two lines.The first line of each test case contains an integer $$$n$$$ ($$$1\leq n\leq 100$$$) — the length of the squares row.The second line of each test case contains a string $$$s$$$ with length $$$n$$$, containing characters 'B', 'R' and '?'. Here 'B' stands for a blue square, 'R' for a red square, and '?' for a blank square.

Output: For each test case, print a line with a string only containing 'B' and 'R', the colors of the squares after painting, which imperfectness is minimized. If there are multiple solutions, print any of them.

Note: In the first test case, if the squares are painted "BRRBRBR", the imperfectness is $$$1$$$ (since squares $$$2$$$ and $$$3$$$ have the same color), which is the minimum possible imperfectness.

Sample Inputs:
5
7
?R???BR
7
???R???
1
?
1
B
10
?R??RB??B?

Sample Outputs:
BRRBRBR
BRBRBRB
B
B
BRRBRBBRBR

Solution (Python):
def solve(n, s):
    s = list(s)
    opp = {'R': 'B', 'B': 'R'}
    i = 0
    while i < n and s[i] == '?':
        i += 1
    if i == n:
        s.append('R')
    for j in range(i - 1, -1, -1):
        if s[j] == '?':
            s[j] = opp[s[j + 1]]
    for k in range(i + 1, n):
        if s[k] == '?':
            s[k] = opp[s[k - 1]]
    print(''.join(s[:n]))
for t in range(int(input())):
    n = int(input())
    s = input()
    solve(n, s)

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    s = input()
    s = list(s)
    count = 0
    for i in range(n):
        if s[i] == 'B':
            if s[i - 1] == '?':
                k = i - 1
                p = k
                while s[k] == '?' and k >= 0:
                    if k % 2 == p % 2:
                        s[k] = 'R'
                    else:
                        s[k] = 'B'
                    k -= 1
            if i != n - 1:
                if s[i + 1] == '?':
                    k = i + 1
                    p = k
                    while k < n and s[k] == '?':
                        if k % 2 == p % 2:
                            s[k] = 'R'
                        else:
                            s[k] = 'B'
                        k += 1
        elif s[i] == 'R':
            if s[i - 1] == '?':
                k = i - 1
                p = k
                while s[k] == '?' and k >= 0:
                    if k % 2 == p % 2:
                        s[k] = 'B'
                    else:
                        s[k] = 'R'
                    k -= 1
            if i != n - 1:
                if s[i + 1] == '?':
                    k = i + 1
                    p = k
                    while k < n and s[k] == '?':
                        if k % 2 == p % 2:
                            s[k] = 'B'
                        else:
                            s[k] = 'R'
                        k += 1
        else:
            count += 1
    if count == n:
        k = 0
        while k < n and s[k] == '?':
            if k % 2 == 0:
                s[k] = 'B'
            else:
                s[k] = 'R'
            k += 1
    s = ''.join(map(str, s))
    print(s)

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    s = input().strip()
    if s == '?' * n:
        s = 'B' + s[1:]
    while '?' in s:
        s = s.replace('R?', 'RB')
        s = s.replace('B?', 'BR')
        s = s.replace('?B', 'RB')
        s = s.replace('?R', 'BR')
    print(s)