For any string
, let
denote the minimum number of key presses Victor needs to do to remove all e
s from
.
For any string
with some letters underlined, let
denote the minimum number of key presses Victor needs to do so that the cursor is at every underlined letter at least once.
Lemma 1.
if
are letters different from e
and
.
Proof. Let
and
.
"
" Let
be some operations on
that visit each letter at least once. At the first point, when the cursor is at the letter
(for
), insert
times the operations hx
. This yields a sequence of
operations on
that delete all the e
s.
"
" Victor has to do the operation x
exactly
times (once for each e
). To move the cursor to some letter e
, he has to move it to the following letter and then issue h
. Therefore, the cursor visits the letters
at least once. 
Using Lemmas 1 and 2, it only remains to find
for strings
not containing the letter e
(with some letters underlined). This is an instance of the travelling salesman problem, but the corresponding graph has some additional structure described by the following two lemmas.
Lemma 2. If the cursor is at some position
and Victor does some sequence of operations h
,
with
(or
), then Victor can instead do a shorter sequence of operations which visit the same letters, apart from at most the letter at position
.
Proof. Let
be the letter at position
. If
f
(or
), then Victor can do the operations
. Otherwise, he can do the operations
. 
Lemma 3. Assume Victor does the minimum number of key presses to visit each underlined letter in
at least once. Then Victor will not do operation h
more than once with the cursor at the same position.
Proof. Otherwise, Victor does the operation h
two times with the cursor at the same position and such that one of the following operations is different from h
. Therefore, Victor could shorten his sequence of operations according to Lemma 3. 
It follows, that Victor gets the minimum number of key presses like in the following picture where each underlined character (represented by a dot in the picture) is contained in one of the intervals spanned by the horizontal arrows from right to left (which can have length
) and each such arrow ends at an underlined letter.
This observation yields an
solution: It is easy to find a suitable recurrence relation for the minimum number
of key presses to visit all underlined letters up to position
with the cursor ending up at position
.
An
solution works roughly as follows:
For simplicity, assume that operation f
moves the cursor infinitely far away to the right if the character
does not appear anywhere to the right of the current cursor position. Apparently, this does not change the answer.
Let
be the minimum number of key presses before or after which the cursor is to the left of
such that the cursor lands on or passes position
exactly once and with operation f
.
Let
be the minimum number of key presses before or after which the cursor is to the left of
such that the cursor lands on or passes position
exactly three times: the first time with operation f
, the second time with operation h
and the third time with operation f
.
Let
be the
character of the string, let

and let

Then the following recurrence relations hold:


Then, we have
where
is an (imaginary) position directly to the right of the last character of
and
is some (imaginary) letter not contained in
.
Remark. The condition that the first character of Victor's document is not an e
is in fact not necessary due to the following lemma.
Lemma 4.
for any
.
Comments