PermRLE (Small)
Time limit5sMemory limit512 MB
Split the string into fixed blocks of length k, permute each block the same way, and minimize the number of runs in the result over all k! permutations.
- Level
Medium6 of 10
- Topics
- Brute force, Sorting, Implementation, String matching
- Solved
- No attempts yet
Problem
You have written a small variation of run-length encoding (RLE) called PermRLE.
To compress a string, this algorithm picks a permutation of the integers from 1 to , applies that permutation to the first letters of the string, then to the next block of letters, and so on to the last block. The length of the string must be divisible by . After every block is rearranged, the new string is compressed with the RLE described below.
Applying a permutation to a block of letters means placing the -th letter of that block in the first position, the -th letter in the second position, and so on up to the -th position. For example, applying the permutation to the block abcd gives cadb. Applying the same permutation block by block to the longer string abcdefghijkl gives cadbgehfkilj.
The rearranged string is then compressed with run-length encoding. To keep things simple, the compressed size of a string is the number of groups of consecutive equal letters. For example, the compressed size of aabcaaaa is 4. The first of the four groups holds two copies of a, the next two groups hold a single b and a single c, and the last is a longer group of four copies of a.
The compressed size depends on the permutation you pick. Since a compression algorithm aims to make the compressed text as small as possible, pick the permutation that gives the smallest compressed size and print that size.
Input
The first line of input gives the number of test cases, . test cases follow.
The first line of each test case contains . The second line contains , the string to be compressed.
Limits
- contains only the lowercase letters
athroughz. - The length of is divisible by .
Output
For each test case, print one line in the form Case #X: Y, where is the number of the test case and is the minimum compressed size of .