TopcoderArchive
SRM5 Oct 2005
SRM 267

WordNumber

Problem statement, definition, constraints, and public examples.

Problem Statement

It is possible to number all the words that could be formed from a specified alphabet. One way to do it is to put the words in order based on length, with words of common length ordered alphabetically. Using this scheme, if we use the alphabet consisting of just the 3 letters 'a', 'b', and 'c' then the words are numbered:
   1:a 2:b 3:c 4:aa 5:ab 6:ac 7:ba 8:bb   etc.
There are an infinite number of possible words, but each has its own positive number.

We want to be able to find the word that corresponds to any given number. Create a class WordNumber that contains a method theWord that is given alpha, the number of letters in the alphabet, and n, the number of a word. It returns the String that is the word corresponding to n.

The alphabet to be used is the first alpha letters of the normal lowercase alphabet, with their usual alphabetical ordering.

Definition

Class:
WordNumber
Method:
theWord
Parameters:
int, int
Returns:
String
Method signature:
String theWord(int alpha, int n)
(be sure your method is public)

Constraints

  • alpha will be between 2 and 26, inclusive.
  • n will be between 1 and 2,000,000,000, inclusive.

Examples

  1. 3
    5
    Returns: "ab"
    See the table above.
  2. 3
    13
    Returns: "aaa"
    Extending the table above, we find that word 12 is "cc" so the next word, word 13, must be the first 3 letter word.
  3. 26
    2000000000
    Returns: "flhomvx"
  4. 26
    456
    Returns: "qn"
Back to all problems