Editorial for COCI '16 Contest 7 #3 Igra
Submitting an official solution before solving the problem yourself is a bannable offence.
We will construct a word letter by letter, starting from the first one, in a way that we try to place the lexicographically smallest letter to the current position. After we choose a letter, we need to check whether the remaining letters can be placed so that none of them match the same letter in Mirko's word (MR). Therefore, we are not interested in the order of these letters, but only if they can be arranged somehow.
Let ,
and
denote the number of remaining letters
a
, b
and c
, respectively, and ,
and
the number of remaining letters
a
, b
and c
in MR.
- Let's try to place
letters
a
to the positions of lettersb
in MR, i.e., reduceand
by
.
- The remaining letters
a
we place to the positions of lettersc
in MR, i.e., reduceby
and set
to
.
- We set the remaining letters
b
in MR to lettersc
, i.e., reduceby
and set
to
.
- Now, we need to place the remaining letters
c
to the positions of lettersa
in MR, i.e., reduceby
and set
to
.
If we couldn't have made any of the previous moves, that means no solution exists for the chosen . If we could have, we are left with checking whether letters
b
can be placed to the positions of letters a
and c
in MR. This will hold if . In that case, the letters can be placed for this
and the check is done.
We will test this out for each between
and
. If the check didn't succeed for any
, then it is impossible to place the remaining letters so that none of them match the same letter in Mirko's word, otherwise we can. This needs to be done for each of the
letters, so the total complexity is
. We leave the linear solution as an exercise for the reader.
Comments