This page is still under construction.

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

Bundling

Interview

Time limit20sMemory limit1024 MB

Summary
Partition N strings into groups of exactly K to maximize the total length of the longest common prefix within each group.
Level

Medium7 of 10

Topics
Trie, Tree, Greedy, DFS
Solved
No attempts yet

Problem

Pip has N strings. Each string consists only of letters from A to Z. Pip would like to bundle their strings into groups of size K. Each string must belong to exactly one group.

The score of a group is equal to the length of the longest prefix shared by all the strings in that group. For example:

  • The group {RAINBOW, RANK, RANDOM, RANK} has a score of 2 (the longest prefix is 'RA').
  • The group {FIRE, FIREBALL, FIREFIGHTER} has a score of 4 (the longest prefix is 'FIRE').
  • The group {ALLOCATION, PLATE, WORKOUT, BUNDLING} has a score of 0 (the longest prefix is "").

Please help Pip bundle their strings into groups of size K, such that the sum of scores of the groups is maximized.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the two integers N and K. Then, N lines follow, each containing one of Pip's strings.

Output

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 maximum sum of scores possible.

Limits

  • 1 ≤ T ≤ 100.
  • 2 ≤ N ≤ 105.
  • 2 ≤ K ≤ N.
  • K divides N.
  • Each of Pip's strings contain at least one character.
  • Each string consists only of letters from A to Z.

Examples2

  1. Example 1

    Input
    2
    2 2
    KICK
    START
    8 2
    G
    G
    GO
    GO
    GOO
    GOO
    GOOO
    GOOO
    
    Expected output
    Case #1: 0
    Case #2: 10
    
  2. Example 2

    Input
    1
    6 3
    RAINBOW
    FIREBALL
    RANK
    RANDOM
    FIREWALL
    FIREFIGHTER
    
    Expected output
    Case #1: 6