Jumping Beans
Time limit1sMemory limit128 MB
Track how beans in a row are rearranged after T seconds of a wrap-around swapping process, printing the final order for each test case.
- Level
Medium7 of 10
- Topics
- Simulation, Math, Implementation, Array
- Solved
- No attempts yet
Problem
jumping beans stand in a row. Every second, one bean jumps. Your task is to determine the final arrangement of the beans after a given number of seconds.
To describe the process, give each bean a distinct uppercase letter and assume the beans initially stand in alphabetical order: A, B, C, and so on. For example, if the beans start in the order ABCD.
- At second 1, bean A jumps and swaps places with the bean on its right (B). The row becomes
BACD. - At second 2 it is B's turn. This time B swaps twice: first with A, then with C, giving
ACBD.
In general, at second the leftmost bean that has jumped the fewest times jumps, and it performs swaps in a row. Each swap exchanges the jumping bean with the bean immediately to its right. When the jumping bean is already in the rightmost position, its swap to the right wraps around: the bean moves to the leftmost position and every other bean shifts one place to the right.
Continuing from ACBD, the leftmost least-jumped bean is C. Because this is second 3, C swaps three times: ACBD → ABCD → ABDC → CABD. At second 4 it is D's turn. At second 5 every bean has jumped exactly once, so the bean that jumps is again the one standing in the leftmost position.
Input
The program is tested on one or more test cases. Each test case is given on a single line containing an integer and a string , where is the number of seconds and is the initial arrangement of the beans. is a non-empty string made of distinct uppercase letters ('A'...'Z').
The last test case is followed by a line containing a single 0.
Output
For each test case, print one line:
k. S
where is the test case number (starting at 1) and is the arrangement of the beans after they have jumped for seconds.