PermRLE (Large)
Time limit5sMemory limit512 MB
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 . It applies that permutation to the first letters of the string, then to the next block of letters, and so on to the end of the string. The length of the string is divisible by . After every block is permuted, the new string is compressed with RLE.
Applying a permutation to a block of letters means putting the -th letter of the 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 {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 . test cases follow.
The first line of each test case contains . The second line contains the string to be compressed.
Limits
- contains only lowercase letters 'a' through 'z'
- The length of is divisible by
Output
For each test case print one line containing "Case #: ", where is the number of the test case and is the minimum compressed size of .