Digits on the Scoreboard
Time limit1.5sMemory limit512 MB
Given a 7-row scoreboard of seven-segment digits, output the scoreboard for the next lexicographic permutation of its digits, or print The End if none exists.
- Level
Medium4 of 10
- Topics
- Implementation, String, Sorting
- Solved
- No attempts yet
Problem
Shinhyeong's school cafeteria has distinct digits written in front of it, and a rectangular scoreboard that is 7 cells tall and cells wide. Each cell holds one light bulb. Each digit is shown with a block of 7 rows by 6 columns of bulbs. The figure below shows a scoreboard with the digits 0 through 9 in order. A black cell is a lit bulb, and an empty cell is an unlit bulb. We assume there is no other way to show the same digit.

Reading the digits on the scoreboard from left to right gives a permutation . Shinhyeong wondered which permutation comes right after in lexicographic order among the permutations that can be made by reordering the elements of . He also wanted to know what the scoreboard looks like for that permutation.

For example, reading the digits in the figure above gives {1, 4, 2}. Listing the permutations of these digits in lexicographic order gives the following.
{1, 2, 4}, {1, 4, 2}, {2, 1, 4}, {2, 4, 1}, {4, 1, 2}, {4, 2, 1}

The permutation after {1, 4, 2} is {2, 1, 4}, so the one Shinhyeong wants is {2, 1, 4}. The scoreboard for it is shown above. Given the scoreboard in front of the cafeteria, write a program that prints the scoreboard for the permutation Shinhyeong is curious about.
Input
The scoreboard is given on 7 lines, each with characters, for digits. A 1 means a lit bulb, and a 0 means an unlit bulb. satisfies .
Output
Given the scoreboard in front of the cafeteria, print the scoreboard for the next permutation in lexicographic order after the permutations of its digits. If there is no next permutation, print The End.