IOI '01 P3 - Twofive

View as PDF

Submit solution

Points: 25 (partial)
Time limit: 0.1s
Memory limit: 32M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

The secret messages between Santa Claus and his little helpers are usually encoded in the 25-language. The 25-alphabet is the same as the Latin alphabet with one exception

  • the letter Z is absent, i.e. the 25-alphabet contains 25 Latin letters from A through Y in the same order as the Latin alphabet. Each word in the 25-language consists of exactly 25 different letters. A word can be written in a 5 \times 5 table filling the rows first; for example, the word ADJPTBEKQUCGLRVFINSWHMOXY will be written as follows:

A valid word in the 25-language has its letters in each row as well as in each column written in ascending order. Thus, the word ADJPTBEKQUCGLRVFINSWHMOXY is a valid word, in contrast to the word ADJPTBEGQUCKLRVFINSWHMOXY (the ascending order is violated in the second column, and in the third column, too).

Santa Claus has a lexicon. His lexicon is the list of all valid 25-language words in ascending order (lexicographically) along with their ordinal numbers starting from 1. For example, in the lexicon ABCDEFGHIJKLMNOPQRSTUVWXY is the word number 1 and ABCDEFGHIJKLMNOPQRSUTVWXY is the word number 2. In
word number 2, U and T are interchanged from their order in word number 1.

Unfortunately, this lexicon is huge. Write a program that determines the ordinal number of an arbitrary given word, and also the word corresponding to a given ordinal number. There are no more than 2^{31} words in the lexicon.

Input Specification

The input consists of two lines. The first line contains a string with one character: a W or an N. If the first line contains a W, then the second line contains a valid 25-language word, that is, a string with 25 characters. If the first line contains an N, then the second line contains the ordinal number of an existing 25-language word.

Output Specification

The output consists of one line. If the second line of the input file contains a 25-language word, then the line of the output file contains the ordinal number of that word. If the second line of the input file contains a number, then the line of the output file contains the 25-language word with that ordinal number.

Sample Input 1


Sample Output 1


Sample Input 2


Sample Output 2



There are no comments at the moment.