Journey (Codeforces 1476D)
Tags: DP, dsu

There are $$$n + 1$$$ cities, numbered from $$$0$$$ to $$$n$$$. $$$n$$$ roads connect these cities, the $$$i$$$-th road connects cities $$$i - 1$$$ and $$$i$$$ ($$$i \in [1, n]$$$).Each road has a direction. The directions are given by a string of $$$n$$$ characters such that each character is either L or R. If the $$$i$$$-th character is L, it means that the $$$i$$$-th road initially goes from the city $$$i$$$ to the city $$$i - 1$$$; otherwise it goes from the city $$$i - 1$$$ to the city $$$i$$$.A traveler would like to visit as many cities of this country as possible. Initially, they will choose some city to start their journey from. Each day, the traveler must go from the city where they currently are to a neighboring city using one of the roads, and they can go along a road only if it is directed in the same direction they are going; i. e., if a road is directed from city $$$i$$$ to the city $$$i + 1$$$, it is possible to travel from $$$i$$$ to $$$i + 1$$$, but not from $$$i + 1$$$ to $$$i$$$. After the traveler moves to a neighboring city, all roads change their directions to the opposite ones. If the traveler cannot go from their current city to a neighboring city, their journey ends; it is also possible to end the journey whenever the traveler wants to.The goal of the traveler is to visit as many different cities as possible (they can visit a city multiple times, but only the first visit is counted). For each city $$$i$$$, calculate the maximum number of different cities the traveler can visit during exactly one journey if they start in the city $$$i$$$. 

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$). The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters, each character is either L or R.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.

Output: For each test case, print $$$n + 1$$$ integers. The $$$i$$$-th integer should be equal to the maximum number of different cities the traveler can visit during one journey if this journey starts in the $$$i$$$-th city.


Sample Inputs:
2
6
LRRRLL
3
LRL

Sample Outputs:
1 3 2 3 1 3 2
1 4 1 4

Solution (Python):
def left(n, string):
    l0 = [0] * (n + 1)
    l1 = [0] * (n + 1)
    for i in range(1, n + 1):
        l0[i] = l1[i - 1] + 1 if string[i - 1] == 'L' else 0
        l1[i] = l0[i - 1] + 1 if string[i - 1] == 'R' else 0
    return l0

def right(n, string):
    r0 = [0] * (n + 1)
    r1 = [0] * (n + 1)
    for i in range(n - 1, -1, -1):
        r0[i] = r1[i + 1] + 1 if string[i] == 'R' else 0
        r1[i] = r0[i + 1] + 1 if string[i] == 'L' else 0
    return r0
for i in range(int(input())):
    n = int(input())
    string = input()
    (r, l) = (right(n, string), left(n, string))
    print(' '.join(map(str, [1 + r[i] + l[i] for i in range(n + 1)])))

Solution (Python):
def journey(n, s):
    dp_odd = [0]
    dp_even = [0]
    for i in range(n):
        if s[i] == 'L':
            dp_odd.append(1 + dp_even[i])
            dp_even.append(0)
        else:
            dp_even.append(1 + dp_odd[i])
            dp_odd.append(0)
    dp_odd_right = [0]
    dp_even_right = [0]
    for i in range(n):
        idx = n - i - 1
        if s[idx] == 'R':
            dp_odd_right.append(1 + dp_even_right[i])
            dp_even_right.append(0)
        else:
            dp_even_right.append(1 + dp_odd_right[i])
            dp_odd_right.append(0)
    dp_odd_right.reverse()
    dp_even_right.reverse()
    res = ''
    for i in range(n + 1):
        res += str(dp_odd[i] + dp_odd_right[i] + 1) + ' '
    return res
if __name__ == '__main__':
    num_test_cases = int(input())
    for i in range(num_test_cases):
        n = int(input())
        s = input()
        print(journey(n, s))

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    directions = input()
    dpl = [i for i in range(n + 1)]
    dpr = [i for i in range(n + 1)]
    for i in range(1, n + 1):
        if directions[i - 1] == 'R':
            continue
        elif i - 2 >= 0 and directions[i - 2] == 'R':
            dpl[i] = dpl[i - 2]
        else:
            dpl[i] = i - 1
    for i in range(n - 1, -1, -1):
        if directions[i] == 'L':
            continue
        elif i + 1 <= n - 1 and directions[i + 1] == 'L':
            dpr[i] = dpr[i + 2]
        else:
            dpr[i] = i + 1
    ans = [dpr[i] - dpl[i] + 1 for i in range(n + 1)]
    print(*ans)