Twofive
Time limit1sMemory limit128 MB
Map between a valid 5x5 standard Young tableau word and its rank, the count of valid words lexicographically before it.
- Level
Hard8 of 10
- Topics
- Combinatorics, Dynamic programming, Math
- Solved
- No attempts yet
Problem
Santa Claus and his helper elves usually encode their secret messages in the 25-language. The 25-alphabet is the same as the Latin alphabet with one exception: the letter Z is missing. So the 25-alphabet consists of exactly the 25 letters from A to Y, in the same order as the Latin alphabet. Every word in the 25-language consists of exactly 25 distinct letters.
A word can be written into a table by filling the rows one after another from the top. For example, the word ADJPTBEKQUCGLRVFINSWHMOXY is written as follows:
A D J P T
B E K Q U
C G L R V
F I N S W
H M O X Y
A word is valid if, when written into the table this way, the letters in every row and in every column are in ascending order. Thus ADJPTBEKQUCGLRVFINSWHMOXY is valid, whereas ADJPTBEGQUCKLRVFINSWHMOXY is not (the ascending order is broken in the second column, and in the third column as well).
Santa Claus keeps a lexicon: the list of all valid 25-language words in lexicographic ascending order, numbered starting from 1. For example, in this lexicon ABCDEFGHIJKLMNOPQRSTUVWXY is word number 1 and ABCDEFGHIJKLMNOPQRSUTVWXY is word number 2; word number 2 is obtained from word number 1 by swapping U and T.
Unfortunately this lexicon is enormous. Write a program that, given a word, determines its ordinal number, and, given an ordinal number, determines the corresponding word. The lexicon contains fewer than words.
Input
The first line contains a single character, either W or N.
- If the first line is
W, the second line contains a valid 25-language word, i.e. a string of 25 characters. - If the first line is
N, the second line contains the ordinal number of an existing 25-language word.
Output
If the second line is a 25-language word (W), output the ordinal number of that word on a single line.
If the second line is an ordinal number (N), output the 25-language word with that ordinal number on a single line.