#P13469. [GCJ 2008 #2] PermRLE

    ID: 15344 远端评测题 10000ms 1024MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>动态规划 DP2008状压 DPGoogle Code Jam

[GCJ 2008 #2] PermRLE

题目描述

You've invented a slight modification of the run-length encoding (RLE) compression algorithm, called PermRLE.

To compress a string, this algorithm chooses some permutation of integers between 11 and kk, applies this permutation to the first kk letters of the given string, then to the next block of kk letters, and so on. The length of the string must be divisible by kk. After permuting all blocks, the new string is compressed using RLE, which is described later.

To apply the given permutation pp to a block of kk letters means to place the p[1]p[1]-th of these letters in the first position, then p[2]p[2]-th of these letters in the second position, and so on. For example, applying the permutation {3,1,4,2}\{3,1,4,2\} to the block "abcd" yields "cadb". Applying it to the longer string "abcdefghij" in blocks yields "cadbgehfik".

The permuted string is then compressed using run-length encoding. To simplify, we will consider the compressed size of the string to be the number of groups of consecutive equal letters. For example, the compressed size of "aabcaaaa" is 44; the first of the four groups is a group of two letters "a", then two groups "b" and "c" each containing only one letter, and finally a longer group of letters "a".

Obviously, the compressed size may depend on the chosen permutation. Since the goal of compression algorithms is to minimize the size of the compressed text, it is your job to choose the permutation that yields the smallest possible compressed size, and output that size.

输入格式

The first line of input gives the number of cases, NN. NN test cases follow.

The first line of each case will contain kk. The second line will contain SS, the string to be compressed.

输出格式

For each test case you should output one line containing "Case #X: Y" (quotes for clarity) where XX is the number of the test case and YY is the minimum compressed size of SS.

2
4
abcabcabcabc
3
abcabcabcabc
Case #1: 7
Case #2: 12

提示

Limits

  • N=20N = 20
  • SS will contain only lowercase letters 'a' through 'z'
  • The length of SS will be divisible by kk

Small dataset (5 Pts, Test set 1 - Visible)

  • 2k52 \leq k \leq 5
  • 1length of S10001 \leq \text{length of } S \leq 1000

Large dataset (30 Pts, Test set 2 - Hidden)

  • 2k162 \leq k \leq 16
  • 1length of S500001 \leq \text{length of } S \leq 50000