Editorial for Baltic OI '05 P4 - Ancient Manuscript


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.

Note that the original editorial was in Lithuanian and may have translation errors.

The problem is solved by generating all variants and applying dynamic programming, to avoid having to recompute everything.

A dynamic table \text{dp}[i_1, r, i_2, i_3, i_4, i_5] is created; It shows the number of ways in which a phrase can be completed if:

  • already completed up to and including the i_1th letter;
  • the i_1th letter is r;

Counting backwards from the i_1th letter (we have no idea what is happening at the beginning of the word only what happens at the end, i.e. at the point where we need to fill in from) is

  • i_2 identical successive consonants
  • i_3 consecutive consonants
  • i_4 identical successive vowels
  • i_5 consecutive vowels

For example, the initial phrase y*af:

We filled it up to aayb in a recursive way, i.e.

  • i_1 = 4,
  • r = b,
  • i_2 = 1 (ending in a single consonant b)
  • i_3 = 2 (ending in two consonants yb);
  • i_4 = 0 (no vowels at the end);
  • i_5 = 0.

Instead of all the *'s, we try to substitute all the possible letters from a to z. For each letter, we check whether it can be done (i.e. whether it does not exceed the set norms), and fill in the table. If we can take a look from the table, we take it from there. If not, it is calculated recursively and stored in the table.


Comments

There are no comments at the moment.