Editorial for COCI '11 Contest 4 #6 Kriptogram
Submitting an official solution before solving the problem yourself is a bannable offence.
We can solve this task by modifying the Knuth-Morris-Pratt (KMP) string searching algorithm. Let's introduce some notation.
- We will denote corresponding words of the encrypted message with
. Also,
will denote the sentence made up of words
through
.
will be words from the sentence of the original text, and
sentence made up of words
through
.
- Let matches(
,
) be boolean function telling us whether
can be decrypted into
. For example, matches(
a b a
,c d c
) = true; matches(a b b
,x y z
) = false.
As in standard KMP, we will calculate the prefix function , but with slightly different meaning.
will be equal to largest possible
such that:
matches(,
) = true1
After finding , we must find
within
. For each word in
, we are interested in the largest suffix that corresponds to some prefix of
. If we encounter a mismatch, we continue with the largest possible prefix of
, which we look up in
.
We must also find a way to efficiently evaluate matches function. We will transform our messages using the following transformation:
if
doesn't appear in
if
is the largest index such that
and
By using and
, we can calculate matches in linear time which is sufficient for obtaining the maximum number of points. Total complexity is also linear.
1 Only difference between this definition and the original one is that we use our matches function instead of standard string comparison.
Comments