Editorial for Baltic OI '13 P6 - Vim


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.

For any string s, let f(s) denote the minimum number of key presses Victor needs to do to remove all es from s.

For any string s with some letters underlined, let g(s) 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. f(l1s1een2l2s2eenklksk)=2(n2++nk)+g(l1s1lksk) if l1,,lk are letters different from e and n2,,nk1.

Proof. Let t=l1s1een2l2s2eenklksk and s=l1s1lksk.

"" Let o1,,op be some operations on s that visit each letter at least once. At the first point, when the cursor is at the letter li (for 2ik), insert ni times the operations hx. This yields a sequence of 2(n2++nk)+p operations on t that delete all the es.

"" Victor has to do the operation x exactly n2++nk 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 l1,,lk at least once.

Using Lemmas 1 and 2, it only remains to find g(s) for strings s 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 p and Victor does some sequence of operations h, o1,,ok with o1h (or k=0), then Victor can instead do a shorter sequence of operations which visit the same letters, apart from at most the letter at position p1.

Proof. Let l be the letter at position p. If o1 fl (or k=0), then Victor can do the operations o1,,ok. Otherwise, he can do the operations o2,,ok.

Lemma 3. Assume Victor does the minimum number of key presses to visit each underlined letter in s 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 0) and each such arrow ends at an underlined letter.

This observation yields an O(N2S) solution: It is easy to find a suitable recurrence relation for the minimum number r(a,b) of key presses to visit all underlined letters up to position a with the cursor ending up at position b.

An O(NS2) solution works roughly as follows:

For simplicity, assume that operation fc moves the cursor infinitely far away to the right if the character c does not appear anywhere to the right of the current cursor position. Apparently, this does not change the answer.

Let p(a,c) be the minimum number of key presses before or after which the cursor is to the left of a such that the cursor lands on or passes position a exactly once and with operation fc.

Let q(a,c,d) be the minimum number of key presses before or after which the cursor is to the left of a such that the cursor lands on or passes position a exactly three times: the first time with operation fc, the second time with operation h and the third time with operation fd.

Let si be the ith character of the string, let

ui={,if the ith character is underlined0,otherwise

and let

κc,d={,c=d0,cd

Then the following recurrence relations hold:

p(a+1,c)=min{p(a,c)+κc,sa+uap(a,sa)+2q(a,sa,c)+κc,saq(a,sa,sa)+2

q(a+1,c,d)=min{p(a,c)+3+κc,sap(a,sa)+5q(a,c,d)+1+κc,sa+κd,saq(a,c,sa)+3+κc,saq(a,sa,d)+3+κd,saq(a,sa,sa)+5

Then, we have g(s)=p(N,k)2 where N is an (imaginary) position directly to the right of the last character of s and k is some (imaginary) letter not contained in s.

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. f(een timess)=n+f(s) for any n0.


Comments

There are no comments at the moment.