TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

FewestStrokes

Problem statement, definition, constraints, and public examples.

Problem Statement

Often, I find myself trying to repeat a certain character a great number of times in a primitive editor. For example, on many occasions I have had to make 50 periods. To type in a single character 50 times is very tedious, so I usually do something like type in the character 5 times, then copy those 5 occurrences of the character, and paste 5 characters at a time until I have 50. This might not be the best way to get 50 though, so in this problem, your task is to determine the optimal way to get a certain number of characters. You may use only the following keys:
  1. The character to be repeated, which will be represented by a period ('.') in this problem.
  2. The arrow keys, which allow you to move the cursor left or right. These will be represented by 'L' and 'R', respectively.
  3. The home and end keys, which move to the beginning and end of the string of characters entered so far, respectively. These will be represented by 'H' and 'E', respectively.
  4. The shift key which, when used in conjunction with the arrow keys or the home and end keys, allows you to select characters. We will represent depressing the shift key by 'D', and releasing it by 'U'. However, we will not count pressing or releasing the shift key as a keystroke, since that can be done without much added trouble.
  5. The key combinations Ctrl-C and Ctrl-V, which allow you to copy and paste text, respectively. These will simply be represented by 'C' and 'V', respectively, and only count as a single keystroke each.
  6. The backspace key allows you to delete one character immediately before (to the left of) the cursor, each time it is pressed. This will be represented by 'B'.
To select text using the shift key, you move the cursor to one end of the region to be selected, which we will call position pos1 (indicating that there are pos1 characters to the left of the cursor). You then hold down the shift key, and move the cursor to the other end of the region you want to select, at position pos2. The text between the pos1 and pos2 is now selected, and, once the shift key is released, abs(pos2-pos1) characters may be copied using Ctrl-C (Ctrl-C has no other effect, other than to copy the selected text). If any key other than home, end, left, or right is pressed while the shift key is depressed, that key has no effect. If any key other than Ctrl-C is pressed while text is selected, the text is first deselected, the cursor position is set to pos2 and then the operation defined by that key is performed. So, if you select text, and then press backspace, the text is first deselected, the cursor position is set to pos2, and then a single character is deleted (or there is no effect if the cursor is at the beginning of the line). When pasting text, the most recently copied number of characters (abs(pos2-pos1)) are inserted at the cursor position, and the cursor moves to the end of the pasted text.

For example, the sequence "....DHUCVVB" will perform the following:
First, we type 4 characters. Next we depress the shift key, and press the home key, and release the shift key, selecting all four characters. We now copy the four characters and paste twice, so there are 12 characters and the cursor is between the 8th and 9th characters. Finally, we press backspace to delete one of them, leaving us with 11 characters in 9 keystrokes (remember not to count 'U' and 'D').

Your task is to compute and return the minimum number of keystrokes required to create exactly n occurrences of a character. Thus, in the above example, we got 11 characters in 9 keystrokes, and since it turns out that there is no way to get 11 characters in less than 9 keystrokes, we would return 9 if n were 11.

Definition

Class:
FewestStrokes
Method:
getStrategy
Parameters:
int
Returns:
int
Method signature:
int getStrategy(int n)
(be sure your method is public)

Notes

  • Pressing left or backspace at the far left of the sequence or right at the far right of the sequence deselects anything that is selected, but has no other effect.
  • The cursor position is always between two adjacent characters or at the beginning or end of the line, not on top of a character (as in some editors).
  • It turns out that, for the given input range, the largest return is less than or equal to 30.

Constraints

  • n will be between 1 and 1,000, inclusive.

Examples

  1. 10
    Returns: 8
    One way to do this in 8 keystrokes is "...DHUCVV." In other words, type three characters, copy them, and then paste twice, and insert a final character.
  2. 23
    Returns: 12
    We can get 24 characters with 11 keystrokes and then press the backspace key: "......DHUCVVVB"
  3. 58
    Returns: 16
    This can be done as: "...DHUCVVVVVVDHUCVV." In other words, insert 3 characters, copy them and paste 6 times to get 21 characters. Then, select 18 of the 21, paste them twice and add a final '.'
  4. 41
    Returns: 14
    We can do this as: "...DHUCVVVVRDHUCVV"
  5. 989
    Returns: 27
    This is a tricky one: "....DHUCVVVVHDELUCVVVDHUCVVVDHUCVVV"
  6. 994
    Returns: 28
    We can do this one as: "...DHUCVVHDEUCVVDHUCVVVVVVDHRUCVVVVV"
  7. 1
    Returns: 1
Back to all problems