This page is still under construction.

Parts of this page are still being built. What you see may change.

PermRLE (Large)

Time limit5sMemory limit512 MB

Summary
Find the permutation of positions 1 to k applied to every block of the string that minimizes the number of runs after run-length encoding.
Level

Hard8 of 10

Topics
Dynamic programming, Bit manipulation, Greedy
Solved
No attempts yet

Problem

You have built a slightly modified run-length encoding (RLE) compression algorithm called PermRLE.

To compress a string, the algorithm first picks a permutation of the integers from 1 to kk. It applies that permutation to the first kk letters of the string, then to the next block of kk letters, and so on to the end of the string. The length of the string is divisible by kk. After every block is permuted, the new string is compressed with RLE.

Applying a permutation pp to a block of kk letters means putting the p[1]p[1]-th letter of the block in the first position, the p[2]p[2]-th letter in the second position, and so on up to the kk-th position. For example, applying the permutation {3,1,4,2} to the block "abcd" gives "cadb". Applying the same permutation block by block to the longer string "abcdefghijkl" gives "cadbgehfkilj".

The permuted string is then compressed with run-length encoding. Here the compressed size is the number of runs of consecutive equal letters. For example, the compressed size of "aabcaaaa" is 4. It splits into a run of two letters 'a', a run holding one 'b', a run holding one 'c', and finally a run of four letters 'a'.

The compressed size depends on the permutation you pick. Choose the permutation that makes the compressed size as small as possible and print that size.

Input

The first line contains the number of test cases NN. NN test cases follow.

The first line of each test case contains kk. The second line contains the string SS to be compressed.

Limits

  • 1≤N≤201 \le N \le 20
  • SS contains only lowercase letters 'a' through 'z'
  • The length of SS is divisible by kk
  • 2≤k≤162 \le k \le 16
  • 1≤∣S∣≤500001 \le |S| \le 50000

Output

For each test case print one line containing "Case #XX: YY", where XX is the number of the test case and YY is the minimum compressed size of SS.

Examples3

  1. Example 1

    Input
    2
    4
    abcabcabcabc
    3
    abcabcabcabc
    
    Expected output
    Case #1: 7
    Case #2: 12
    
  2. Example 2

    Input
    3
    2
    ab
    2
    aa
    5
    aaaaa
    
    Expected output
    Case #1: 2
    Case #2: 1
    Case #3: 1
    
  3. Example 3

    Input
    3
    16
    zyxwvutsrqponmlk
    16
    aabbccddeeffgghh
    16
    aaaaaaaaaaaaaaaa
    
    Expected output
    Case #1: 16
    Case #2: 8
    Case #3: 1