10553 - 二五语言

有一种奇怪的语言叫做“二五语言”。它的每个单词都由A~Y这25个字母各一个组成。但是,并不是任何一种排列都是一个合法的二五语言单词。二五语言的单词必须满足这样一个条件:把它的25个字母排成一个5*5的矩阵,它的每一行和每一列都必须是递增的。比如单词ACEPTBDHQUFJMRWGKNSXILOVY,它排成的矩阵如下所示:

A C E P T B D H Q U F J M R W G K N S X I L O V Y

因为它的每行每列都是递增的,所以它是一个合法的单词。而单词YXWVUTSRQPONMLKJIHGFEDCBA则显然不合法。 由于单词太长存储不便,需要给每一个单词编一个码。编码方法如下:写出单词中字母A在矩阵中的行号和列号,再写出B的行号和列号……依此类推。得到的序列 叫做行列序列。如上面的合法单词的行列序列是:11 21 12 22 13 31 41 23 51 32 42 52 33 43 53 14 24 34 44 15 25 54 35 45 55。然后把所有合法单词的行列序列排序。一个单词的行列序列排序后的位置,就是这个单词的编码。比如,单词ABCDEFGHIJKLMNOPQRSTUVWXY的编码为1,而单词ABCDEFGHIJKLMNOPQRSUTVWXY的编码为2。

现在,你需要编一个程序,完成单词与编码间的转换。

输入

第一行为一个字母N或W。N表示把编码转换为单词,W表示把单词转换为编码。 若第一行为N,则第二行为一个整数,表示单词的编码。若第一行为W,则第二行为一个合法的单词。

输出

每行一个整数或单词。

样例

输入

N
2

输出

ABCDEFGHIJKLMNOPQRSUTVWXY

输入

W 
ABCDEFGHIJKLMNOPQRSUTVWXY

输出

2

提示

Twofive IOI 2001 In order to teach her young calvess the order of the letters in the alphabet, Bessie has come up with a game to play with them. The calves are given a 5 x 5 grid on which they can put the letters 'A'-'Y', but the one rule is that all the letters going across the columns and down the rows must be in the order they appear in the alphabet.

There are a huge number of possible grids, so Bessie decides that they will be named by the string of letters that is given by reading across each row, going down the rows. For example, the grid:

 A B C D E
 F G H I J
 K L M N O
 P Q R S T 
 U V W X Y

would have the name ABCDEFGHIJKLMNOPQRSTUVWXY, which is coincidentally the first possible grid when the entire set of grids is ordered alphabetically. The second grid that meets this requirement is ABCDEFGHIJKLMNOPQRSUTVWXY, which is formed by switching the 'T' and 'U' in the above grid. Help the calves gain bragging rights. Given a number, M, find which string is Mth in the list of all possible grids when they are sorted alphabetically, and, given a string of letters, find out what number the corresponding grid is in the list of all possible grids.

PROGRAM NAME: twofive INPUT FORMAT The first input line contains one of two letters, either an 'N' or a 'W'.

If the first input line contains an 'N', the second line will contain an integer, M, that corresponds to the number of a valid grid. If the first line contains a 'W', the second line will contain a string of 25 letters, which represents a valid grid.

OUTPUT FORMAT If the input contained the number of a valid grid (first line 'N'), the output should contain a string of 25 letters on a line, which corresponds to the Mth grid in the sorted list of all possible grids.

If the input contained a string of letters indicating a grid (first line 'W'), the output should contain a single integer on a line, which corresponds to the number of the given grid in the list of all possible grids.

SAMPLE INPUT #1 (file twofive.in) N 2

SAMPLE OUTPUT #1 (file twofive.out) ABCDEFGHIJKLMNOPQRSUTVWXY

SAMPLE INPUT #2 (file twofive.in) W ABCDEFGHIJKLMNOPQRSUTVWXY

SAMPLE OUTPUT #2 (file twofive.out) 2

时间限制 1 秒
内存限制 128 MB
讨论 统计
上一题 下一题