here is my algorithm (sorry for my english):
1) compute the length of the original string and the number of ones and zeroes in it
2)make a table of the fragments(with width of the original string). Align each fragment to the left and to the right:
110***
***110
11****
****11
3)count the number of ones and zeroes in each column. If ones > zeroes then in the string there is 1 in this position;
the same for zeroes;
if ones == zeroes then it can be any of it(according to the counted number of 1 and 0 in the original string)
I used a brute force approach to find the correct fragment combination. Complexity = O(n^2).
Here is the pseudocode:
1) Use a map<string, int> to store the combined fragments and a count of how many fragments of that kind can be made.
2) Concatenate fragment with fragment[j], where 0 <= i, j <= number of fragments and i <> j.
3) Increment the count of map<string, int>
Solution:
The fragment we are looking for is the one having the greatest count.
I solved this problem with backtracking in 0.015. A hint to those who are lost: for each test case compute the length of the solution string by summing all the input string lengths and dividing by (number of lines / 2) and exploit this in your pruning.