Fixing Codes
Time limit1sMemory limit128 MB
Given a prefix-free code and a new binary string, find the minimum total number of bits to append so the whole multiset becomes prefix-free again.
- Level
Hard8 of 10
- Topics
- Greedy, Tree, DFS, Bit manipulation
- Solved
- No attempts yet
Problem
A binary string is a string over the alphabet . A code is a multiset of binary strings (the same string may appear any number of times). A code is a fixed code if none of its strings is a prefix of another string in it.
A code is extended to a code if is a prefix of for every . The cost of this extension is , where is the length (number of characters) of string .
You are given a fixed code and a new binary string . Find the minimum cost needed to extend into a fixed code. In other words, append the fewest possible bits to zero or more of the strings in so that the whole collection becomes a fixed code.
Input
The first line contains an integer (), the number of test cases. Each of the next lines contains one or more binary strings separated by spaces. Each line has at most 41 strings, and every string has length at most 40. On each line the last string is the new incoming string , and the remaining strings form the fixed code for that test case.
Output
For each test case, print the minimum cost to extend into a fixed code on its own line.