#P2750. Conversion between Valid Erwu-Five Words and Their Codes
Conversion between Valid Erwu-Five Words and Their Codes
Conversion between Valid Erwu-Five Words and Their Codes
There is a strange language called Erwu-Five Language in which every valid word is a permutation of the 25 letters A to Y, with each letter occurring exactly once. However, not every permutation is valid. A valid word must satisfy the following condition: when its 25 letters are arranged into a 5×5 matrix in row‐major order, every row and every column is strictly increasing. In other words, if we denote the letter at row i and column j by \(a_{i,j}\), then for all valid i and j we have:
\[ a_{i,j} < a_{i,j+1} \quad \text{and} \quad a_{i,j} < a_{i+1,j} \]
For example, the word ACEPTBDHQUFJMRWGKNSXILOVY
forms the matrix:
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
Since each row and column is in increasing order, the word is valid. In contrast, the word YXWVUTSRQPONMLKJIHGFEDCBA
is not valid.
To facilitate storage, each valid word is assigned a unique code. The code is defined by taking all valid words, ordering them lexicographically, and then assigning the rank (starting from 1) to each word. For example, the word ABCDEFGHIJKLMNOPQRSTUVWXY
has rank \(1\) (its code is 1), and ABCDEFGHIJKLMNOPQRSUTVWXY
is the second word (code 2).
Your task is to convert between a valid word and its code. If the input is a valid 25-letter word, output its corresponding code. If the input is a number (representing a code), output the valid word corresponding to that rank.
inputFormat
The input consists of a single line. It will contain either a 25-character string (formed only by letters A to Y, each letter exactly once and satisfying the 5×5 increasing condition) or a positive integer representing the code.
If the input is a string, output the integer code (rank) of that word. If the input is an integer, output the valid word corresponding to that rank.
outputFormat
Output a single line containing the conversion result. If the input was a word, output its integer code. If the input was a code, output its corresponding 25-letter word.
sample
ABCDEFGHIJKLMNOPQRSTUVWXY
1