En oding
day 3, sour e le en oding.*, available memory 256 MB CPSPC 2004, June 10
In most programming languagues, string
onstants
an
ontain
hara
ters not a
essible dire
tly
from the keyboard. This
an be a
hieved by using a spe
ial marker symbol, usually a ba
kslash.
Sometimes there are more spe
ial symbols having dierent fun
tions. Most of the string is read
verbatim, as it is, and the rest is interpreted in one way or another.
Prof. P. Entropi
, a famous
omputer s
ientist, has his own ideas on this. He believes that using
a xed symbol (or a xed set of symbols) as a marker is extremely inexible and
annot be the best
solution. For what if you need to en
ode a string with many ba
kslashes?
Professor has invented a dynami
oding method, in whi
h verbatim and interpreted parts of
the string are de
lared expli
itly. The idea is to introdu
e an additional marker, whi
h marks the
beginning and the end of every verbatim part of the string. The symbol that is used for this marker
is dynami
, i.e. it
an
hange inside the string. At the beginning of the string there is no marker
and the symbols are interpreted. As soon as some symbol x appears twi
e in a row, it be
omes
the spe
ial marker and the xx just read is dis
arded. From now on, every appearan
e of x swit
hes
between the verbatim mode and the interpreted mode. If some other symbol y appears twi
e in a
row anywhere in the interpreted mode, the marker is
hanged to y, and so on.
For example the string ``
ab
ab
aa
bb
aa
'' is read as ``abABaaCC'', where the
apitalized
part of the string is interpreted and the rest is read verbatim.
Prof. Entropi
needs to know how e
ient dynami
oding really is in terms of spa
e. He has
gathered a representative sample of strings with parts marked verbatim and interpreted. He
wants to nd the shortest possible en
odings of these strings using the dynami
oding method.
Your task is to write a
omputer program that does exa
tly that.
Task
Write a program, that:
reads the size of the alphabet and a string from the input le,
Input
The rst line of the input le en
oding.in
ontains a single integer k , 2 k 26 the size of the
alphabet. The alphabet
onsists of the rst k letters of the standard english alphabet. The se
ond
line
ontains a string to be en
oded. The part to be read verbatim is written with small letters and
the part to be interpreted is written with
apital letters. The length of the string is at most 100 000
hara
ters.
1
Output
The rst and only line of the output le en
oding.out should
ontain a single string (one of )
the shortest en
oding(s) of the input string using the given alphabet.
Example
For the input le en
oding.in:
3
abABaaCC
one of possible
orre
t answers is the le en
oding.out:
ab
ab
aa
bb
bb