How to reconstruct a string from its substrings in CodeForces 828C problem?