Vim
Time limit2sMemory limit512 MB
Given a string over a-j, find the minimum Vim keypresses (x, h, and f C) to delete every 'e' without touching other characters, starting with the cursor at index 0.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, String, Implementation
- Solved
- No attempts yet
Problem
The American novelist Ernest Vincent Wright wrote the novel Gadsby without ever using the letter e.
Sanggeun's favorite novelist is Ernest too. These days he writes his own novel using only the first ten letters of the alphabet (a through j).
One day the e key on Sanggeun's keyboard broke. He has no money for a new keyboard, so he decides to delete every e already typed in his manuscript. His friend Seonyoung tells him that this is easy to do in Vim.
Sanggeun knows only three Vim commands: x, h, and f.
xdeletes the character under the cursor. After the deletion the cursor keeps the same position (index), so it now points at the character that used to follow the deleted one. Thereforexcannot be used while the cursor is on the last character of the document.hmoves the cursor one position to the left. If the cursor is already on the first character, it does not move.fmust be followed by another characterC. It moves the cursor to the nearestCthat lies strictly to the right of the cursor. If there is no suchC, the cursor stays where it is.
Each x and each h counts as one keypress; an f command counts as two keypresses (the f plus the following character).
For example, suppose the text and the cursor are as below, where the character under the cursor is shown in brackets [ ]:
jeff[i]ehadabigidea
- Pressing
xgivesjeff[e]hadabigidea. - Pressing
hgivesjef[f]iehadabigidea. - Pressing
figivesjeffiehadab[i]gidea.
Given a document, write a program that computes the minimum number of keypresses needed to delete every e in it. No character other than e may be deleted. The cursor starts on the first character of the document. Because the e key is broken, the command fe cannot be used.
Input
The first line contains the length N of the document. The second line contains the document itself. The document consists only of lowercase letters from a to j, and its first and last characters are not e. ()
Output
Print, on the first line, the minimum number of keypresses needed to delete every e in the document.
Hint
For the first example document, pressing fdhxhhxffhxfahxhhhxhhhxfdhxfghxfahhx (36 keypresses) removes every e.
All of the commands described in this problem are real Vim commands.