#P16628. [GKS 2017 #E] Copy & Paste

[GKS 2017 #E] Copy & Paste

Problem Description

You want to create a certain target string SS, which consists only of lowercase English letters. You start with an empty string, and you are allowed to perform the following operations:

  • Add any single lowercase letter to the end of your string.
  • Copy any substring of your string (that is, all of the characters between some start point in your string and some end point in your string) to the clipboard. Doing this overwrites whatever was in the clipboard before. The clipboard starts off empty.
  • Add the entire contents of the clipboard to the end of your string. (The contents of the clipboard do not change.)

What is the smallest number of operations needed to create your target string? Note that you must create exactly the target string, with no additional letters.

Input Format

The first line of the input gives the number of test cases, TT. TT lines follow. Each line contains the target string SS.

Output Format

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the minimum number of operations (as described in the problem statement) needed to create the target string.

3
abcabab
aaaaaaaaaaa
vnsdmvnsnsdmkvdmkvnsdmk
Case #1: 6
Case #2: 7
Case #3: 15

Hint

The optimal solution for Sample Case #1 is:

  1. Type aa.
  2. Type bb.
  3. Type cc.
  4. Copy abab to the clipboard.
  5. Paste abab at the end of the string.
  6. Paste abab at the end of the string.

The optimal solution for Sample Case #2 is:

  1. Type aa.
  2. Type aa.
  3. Type aa.
  4. Copy aaaaaa to the clipboard.
  5. Paste aaaaaa at the end of the string.
  6. Copy aaaaaaaaaa to the clipboard.
  7. Paste aaaaaaaaaa at the end of the string.

Limits

SS consists only of lowercase English letters in the range a through z.

Small dataset (Test set 1 - Visible)

1T1001 \le T \le 100.

11 \le length of S6S \le 6.

Large dataset (Test set 2 - Hidden)

1T1001 \le T \le 100.

11 \le length of S300S \le 300.