#P1942. Binary Word Restoration
Binary Word Restoration
Binary Word Restoration
A sender transmits a binary word through a tunnel. Initially, each word is composed of 0's and 1's and has a fixed length n (4 ≤ n ≤ 1000). Moreover, every original word has the following property: if you sum up the positions (1-indexed) of all the 1's in the word, the result is either 0 or a multiple of \(n+1\) (i.e. if we denote the sum by \(S\), then \(S \equiv 0 \pmod{n+1}\)).
However, during transmission through the tunnel, exactly one of the following modifications may occur:
- Any one occurrence of
0
may be changed into a1
(substitution error). - Any one symbol (either
0
or1
) may be deleted (deletion error). - A symbol (either
0
or1
) may be inserted at any position (insertion error). - No change occurs (i.e. the word remains intact).
Your task is to recover the original word given the received word. Note that the received word may have length n, n-1, or n+1 depending on the operation error that occurred. It is guaranteed that there is a unique original word S of length n (which contains only characters 0
and 1
and satisfies \(\sum_{i=1}^{n} i \cdot [S_i = '1'] \equiv 0 \pmod{n+1}\)) such that the received word can be obtained from S by exactly one of the four operations described above.
Note: In all formulas, positions in a word are numbered from 1 to n. The notation \([S_i = '1']\) equals 1 if the i-th character is 1
and 0 otherwise.
inputFormat
The first line contains an integer n (4 ≤ n ≤ 1000).
The second line contains a binary string representing the received word. Its length is either n-1, n, or n+1.
outputFormat
Output the recovered original binary word of length n that satisfies the specified property.
sample
4
1011
1001
</p>