Type Two de Bruijn Sequences
Time limit2sMemory limit512 MB
Given a binary string, append the fewest digits so that every length-n binary word appears as a subsequence.
- Level
Medium7 of 10
- Topics
- Greedy, String, Dynamic programming
- Solved
- No attempts yet
Problem
A word made up of characters 0 and 1 is called a de Bruijn sequence of order if every -character binary word appears as one of its subwords, that is a fragment of consecutive characters of . For example, 0001011100 is a de Bruijn sequence of order 3.
A type two de Bruijn sequence of order is a word of arbitrary length such that every -character binary word appears as a subsequence of , that is a fragment of characters that are not necessarily consecutive. For example, 00101101 is a type two de Bruijn sequence of order 3. As far as we know, Nicolaas Govert de Bruijn did not invent such sequences, but their definition is clearly similar to the original one.
Consider a word made up only of zeroes and ones. How many digits (0 or 1) must be appended to the end of so that it becomes a type two de Bruijn sequence of order ?
Input
The first line contains two integers and () separated by a single space. The second line contains the -character word , made up only of the digits 0 and 1, with no spaces.
Output
Print a single non-negative integer: the minimal number of digits that must be appended to the end of so that it becomes a type two de Bruijn sequence of order .
Hint
For 00101 with , appending the two characters 01 gives 0010101, which is a type two de Bruijn sequence of order 3. Hence the answer is 2.