#P1246. Encoding Ascending Words
Encoding Ascending Words
Encoding Ascending Words
This problem involves encoding words using a simple encoding scheme. Consider the 26 letters \(\mathtt{a,b,c,\dots,z}\). A word is defined as a sequence of at most 6 letters, with the letters strictly in ascending order (i.e. each letter is strictly greater than the previous one). All such words are arranged in lexicographic (dictionary) order, and the encoding of a word is its 1-indexed position in this ordered list.
For example:
- \(a \to 1\)
- \(b \to 2\)
- \(z \to 26\)
- \(ab \to 27\)
- \(ac \to 28\)
Your task is to compute the encoding for the given word. The encoding is defined as follows:
\( \text{encoding}(w) = \sum_{k=1}^{|w|-1} \binom{26}{k} + \text{rank}_{|w|}(w) \)
where \(\text{rank}_{|w|}(w)\) is the lexicographic rank of the word among all words of the same length that satisfy the ascending condition. For each letter in the word at position \(i\), you count the number of valid choices with a letter smaller than \(w[i]\) (and greater than the previous chosen letter) and then multiply by the appropriate binomial coefficient for the remaining positions.
inputFormat
The input consists of a single line containing a word. The word is composed of lowercase English letters in strictly ascending order and its length is at most 6.
outputFormat
Output a single integer — the encoding (1-indexed position) of the word in the sorted list of all valid words.
sample
a
1