Permutation with no spaces
InterviewTime limit1sMemory limit256 MB
Recover the permutation from 1 to N whose decimal forms concatenate to the given digit string, choosing the lexicographically smallest one on ties.
- Level
Medium5 of 10
- Topics
- Backtracking, Brute force, String
- Solved
- No attempts yet
Problem
A permutation that uses each of the numbers through once was written in decimal on one line, with a single space between neighboring numbers, and saved to a file. Someone then deleted every space in that file, so all that is left is one long run of digits.
Put the spaces back and recover the permutation.
Input
The first line contains the digit string that remains after every space was deleted.
The string is a permutation of the numbers through concatenated from front to back, where . The value of is not given. The string can always be split back into such a permutation.
Output
Print the recovered permutation on one line, with a single space between neighboring numbers. Do not forget the spaces.
When more than one permutation fits the string, compare two candidates number by number from the front and print the one whose number is smaller at the first position where they differ. That is, print the lexicographically smallest of the valid permutations.