Blue Gene, Jr.
Time limit1sMemory limit128 MB
Simulate the recursive mutation rules on each short alphanumeric code and print the stabilized code.
- Level
Medium5 of 10
- Topics
- Recursion, Simulation, String
- Solved
- No attempts yet
Problem
Inspired by IBM's Blue Gene project, the CEO of Universal Biological Machinery (UBM) has called on you, UBM's top software engineer. The company's privately subsidized space program found a virus on Mars, the Areopagusvirus, and you have to write a program that calculates how it mutates.
Input
The input is a non-empty series of up to 100 data sets. Each data set follows the description below, and no blank lines separate the data sets.
A single data set has three parts.
- Start line: a single line
START N, where . - Viral code: a single line of alphanumeric characters. An alphanumeric character is an uppercase letter A to Z, or a digit 0 to 9.
- End line: a single line
END.
A single line ENDOFINPUT follows the final data set.
Output
For each data set there is exactly one output set, and no blank lines separate the output sets.
A single output set is one line holding the viral code after it has stabilized through mutating. The viral code mutates according to the following rules.
- The first viral segment to mutate begins with the first alphanumeric character of the viral code and ends with the rightmost alphanumeric character of the code.
- If the first alphanumeric character of a viral segment is a letter, that character is unstable, and it mutates into , the number of mutations that occur in the viral segment immediately to its right (see rule 5). If is greater than 9, the character mutates into the remainder of divided by 10. If there is no viral segment immediately to the right of the unstable character, it mutates into 0.
- If the first alphanumeric character of a viral segment is a digit from 1 to 9, that character is also unstable, and it mutates into . It also makes the viral segment that begins with the alphanumeric character positions to its right and ends with the rightmost alphanumeric character of the viral code mutate. If there is no alphanumeric character positions to its right, then the viral segment immediately to its right (see rule 5) mutates, if one exists.
- If the first alphanumeric character of a viral segment is 0, that character is stable and does not mutate. The character stays 0, and no mutation is counted.
- The viral segment immediately to the right of an alphanumeric character begins with the alphanumeric character one position to its right and ends with the rightmost alphanumeric character of the viral code.