Javanese Cryptanalysis

Time limit1sMemory limit128 MB

Summary
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.
Level

Medium6 of 10

Topics
Graph, BFS, Greedy
Solved
No attempts yet

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 ss is made of words, and each word contains only uppercase letters. Call ss legitimate if, in every word of ss, vowels and consonants alternate (no two vowels are adjacent and no two consonants are adjacent).

A simple substitution cipher is applied to ss: a bijection ff on the 26 uppercase letters is chosen, and the encoded text tt is obtained from ss by replacing every letter cc with f(c)f(c). Because ff 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 tt. Decide whether some legitimate text ss could have been encoded into tt. 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 tt: 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 100 000100\,000 characters.

Output

If no legitimate text ss can be encoded into tt, print the single word impossible.

Otherwise print the decoded legitimate text ss, keeping exactly the same layout as tt: replace every letter, but leave every space and line break unchanged and in its original position. Several legitimate texts may encode into tt; 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 A<B<⋯<ZA < B < \dots < Z) is the smaller text. Every letter of ss is uppercase.

Examples2

  1. Example 1

    Input
    O RISK LIP FOCUS LUCKY
    
    Expected output
    A BECI DEF GAHOC DOHIJ
    
  2. Example 2

    Input
    NEERC
    
    Expected output
    impossible