Javanese Cryptanalysis
Time limit1sMemory limit128 MB
Given a ciphered text, determine a 2-coloring of the 26 letters into vowels and consonants consistent with alternation constraints from adjacent letters in every word, and output the lexicographically smallest valid decoding using graph bipartition and greedy label assignment.
Problem
Javanese is the language of the people living in the central and eastern parts of the island of Java, Indonesia.
In 1926 a standard orthography was created for Javanese using the English alphabet. It uses all 26 letters from A to Z. The five letters A, E, I, O, and U are vowels; the other 21 letters are consonants. In every Javanese word vowels and consonants always alternate — no two vowels and no two consonants are ever next to each other. This property is very useful when deciphering encrypted Javanese text.
A text is made of words, and each word contains only uppercase letters. Call legitimate if, in every word of , vowels and consonants alternate (no two vowels are adjacent and no two consonants are adjacent).
A simple substitution cipher is applied to : a bijection on the 26 uppercase letters is chosen, and the encoded text is obtained from by replacing every letter with . Because is a bijection, exactly 5 letters of the alphabet are images of vowels and the other 21 are images of consonants.
You are given the encoded text . Decide whether some legitimate text could have been encoded into . If several legitimate texts are possible, you must report the single canonical one defined in the Output section.
Input
The input is the encoded text : a list of words separated by single spaces and/or line breaks. Each word consists only of uppercase letters (A–Z).
The input contains at most characters.
Output
If no legitimate text can be encoded into , print the single word impossible.
Otherwise print the decoded legitimate text , keeping exactly the same layout as : replace every letter, but leave every space and line break unchanged and in its original position. Several legitimate texts may encode into ; print the lexicographically smallest one. To compare two candidate texts, read them character by character from the beginning; at the first position where they differ, the text with the smaller letter (with ) is the smaller text. Every letter of is uppercase.