Binary String Reconstruction (Codeforces 1352F)
Tags: Math, dfs and similar

For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers:  $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$;  $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$;  $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.

Input: The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then test cases follow.Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \le n_0, n_1, n_2 \le 100$$$; $$$n_0 + n_1 + n_2 > 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.

Output: Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.


Sample Inputs:
7
1 3 5
1 1 1
3 9 3
0 1 0
3 1 2
0 0 3
2 0 0

Sample Outputs:
1110011110
0011
0110001100101011
10
0000111
1111
000

Solution (Python):
T = int(input())
for _ in range(T):
    (n0, n1, n2) = map(int, input().split())
    if n0 == 0 and n1 == 0:
        print('1' * (n2 + 1))
    elif n1 == 0 and n2 == 0:
        print('0' * (n0 + 1))
    else:
        ans = ''
        if n1 % 2 == 0:
            ci = n1 // 2
        else:
            ci = (n1 + 1) // 2
        ans = ans + '0' * n0 + '01' * ci + '1' * n2
        if n1 % 2 == 0:
            ans = ans + '0'
        print(ans)

Solution (Python):
t = int(input())
for i in range(t):
    (n0, n1, n2) = map(int, input().split())
    if n1 != 0:
        s = ('10' * n1)[:n1 + 1]
        if n1 % 2 == 0:
            print('1' * n2 + s[:n1] + '0' * n0 + '1')
        else:
            print('1' * n2 + s + '0' * n0)
    elif n0 != 0:
        print('0' * (n0 + 1))
    else:
        print('1' * (n2 + 1))

Solution (Python):
def solve():
    (a, b, c) = map(int, input().split())
    if b == 0 and c == 0:
        print('0' * a + '0')
        return
    if a == 0 and b == 0:
        print('1' * c + '1')
        return
    ans = '1' * c + '1'
    huehue = False
    times = b
    if b % 2 == 0:
        times -= 1
    for i in range(times):
        if huehue:
            ans += '1'
        else:
            ans += '0'
        huehue = not huehue
    for i in range(a):
        ans += '0'
    if times == 0:
        ans += '0'
    if b > times and b > 0:
        ans += '1'
    print(ans)
for _ in range(int(input())):
    solve()