Editorial for APIO '08 P3 - DNA
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
First, we ignore the templates and only count the number of sequences of form-.
Let denote the number of sequences of form-
of length
beginning with
. Note that if the second character
comes before
, possible sequences of form-
are those constructed by concatenating
with form-
sequences beginning with
. On the other hand, if
is
or comes after
, we can concatenate
with any form-
sequence.
Thus, we have the following recurrence:
This idea can be extended to count the number of sequences that also match the templates. Table can be computed in time
.
Given , we can trace back the computation of
to find the required
-th gene sequence.
Comments