ambigram

A sequence of characters is called an ambigram if it looks the same when we rotate the piece of paper it is written on 180 degrees around a perpendicular axis on its surface. Being given a sequence of characters we want to transform it into an ambigram, by modifying or deleting some letters.

The given sequence is written in capital English letters. We notice that:

A few examples of ambigrams: MOW, XXXXXXXX, XMIWX, HXHXHXH.

We want to make the smallest possible number of modifications on the given sequence in order to produce an ambigram. Allowed modifications are:

Task

Write a program that determines the ambigram for a given sequence, at minimum cost, by using the operations above. If there is more than one ambigram having the same minimum cost the longest one will be determined. If there still is more than one solution the first one, in alphabetical (lexical-graphical) order, will be determined. An empty sequence is not an ambigram.

Input Data

Input file ambigram.in contains on the first line the sequence for which the ambigram has to be determined.

Output Data

The output file ambigram.out will contain on the first line a positive integer representing the minimum cost to transform the given word into an ambigram, and on the second line a sequence representing the ambigram obtained from the given sequence.

Constraints

Example

ambigram.in ambigram.out Explanations
BXAJ

25
WM

  • delete B at cost 2
  • replace X with W at cost 1
  • delete A at cost 1
  • replace J with M at cost 3

Note: For the same cost we also get ambigram I (delete B, X and A and replace J with I), but the length of this sequence is smaller.

Time limit: 0.1 seconds/test

 

prof. Carmen Popescu
"Gheorghe Lazar" Sibiu National High School
carmen_cngl@yahoo.com