| <p> | |
| You have a list of <strong>N</strong> distinct words, consisting of only lowercase letters. You'd like to print any <strong>K</strong> words from this list, one per page, in any order. | |
| </p> | |
| <p> | |
| You will accomplish this using a very basic text editor. It supports 3 types of operations: typing a letter, deleting the previous letter, and printing the current document. | |
| Note that it does not allow the cursor to be moved! This means that the first operation may only add a letter to the end of the document, and the second may only delete the last letter (if any). | |
| Due to issues with memory leakage, you also need to remember to leave the document completely empty after you've printed your <strong>K</strong> words! | |
| </p> | |
| <p> | |
| What's the minimum number of operations required to get the job done? | |
| </p> | |
| <p> | |
| As an example, let's say that you want to print 3 of the following 5 words: | |
| </p> | |
| <pre> | |
| a | |
| hair | |
| box | |
| awesome | |
| hail | |
| </pre> | |
| <p> | |
| One optimal sequence of 15 operations is as follows: | |
| </p> | |
| <p> | |
| <ul> | |
| <li> - type 'h', 'a', 'i', and 'r' (document = 'hair') | |
| <li> - print | |
| <li> - backspace (document = 'hai') | |
| <li> - type 'l' (document = 'hail') | |
| <li> - print | |
| <li> - backspace 4 times (document = empty) | |
| <li> - type 'a' (document = 'a') | |
| <li> - print | |
| <li> - backspace (document = empty) | |
| </ul> | |
| </p> | |
| <h3>Input</h3> | |
| <p> | |
| Input begins with an integer <strong>T</strong>, the number of sets of words you want to print. | |
| For each set, there is first a line containing the space-separated integers <strong>N</strong> and <strong>K</strong>. | |
| The next <strong>N</strong> lines contain the set of words, one per line. | |
| </p> | |
| <h3>Output</h3> | |
| <p> | |
| For the <strong>i</strong>th set of words, print a line containing "Case #<strong>i</strong>: " followed by | |
| the minimum number of operations required to print <strong>K</strong> of the words and then leave the document empty. | |
| </p> | |
| <h3>Constraints</h3> | |
| <p> | |
| 1 ≤ <strong>T</strong> ≤ 100 <br /> | |
| 1 ≤ <strong>K</strong> ≤ <strong>N</strong> ≤ 300 <br /> | |
| The total length of all <strong>N</strong> words in each set will be at most 100,000 characters.<br /> | |
| </p> | |