Numerically Speaking
Time limit1sMemory limit128 MB
Translate between lowercase words of length 1 to 20 and their bijective base-26 numbers, printing output in a padded two-column format.
- Level
Medium4 of 10
- Topics
- Math, String, Implementation, Simulation
- Solved
- No attempts yet
Problem
Every lowercase word whose length is between 1 and 20 is assigned a distinct positive integer. Words are ordered first by length, and within the same length alphabetically, then numbered consecutively starting from 1. The beginning of the list looks like this:
a 1
b 2
...
z 26
aa 27
ab 28
...
snowfall 157,118,051,752
...
This is exactly bijective base-26 numbering: read the word from left to right using a = 1, b = 2, …, z = 26 as digits, and the number of a word of length with digit values is .
Write a program that translates in both directions: given a word, print its number; given a number, print its word.
Input
The input lists one item per line, each starting in column one. A line whose column one contains a single asterisk (*) marks the end of the input.
Each item is either a number or a word:
- A number consists only of the decimal digits
0–9(there are no commas in the input) and corresponds to some word of length 1 to 20. - A word consists of 1 to 20 lowercase letters
a–z.
Output
Print one line for each input item. The word begins in column one and its number begins in column 23; fill the space between them with blanks (that is, place the word in a left-justified field 22 characters wide, immediately followed by the number). Group the digits of the number with commas every three digits from the right (thousands, millions, and so on).