TopcoderArchive
Archive
Algorithm problem

SequenceSerializer

Problem statement, definition, constraints, and public examples.

Problem Statement

When transmitting data over a network, the data needs to be serialized into a sequence of bits - zeros and ones. Consider the task of serializing a sequence of integers. The simplest way to do this is to transmit a fixed number of bits per integer. In this problem, however, we will consider another method. First, we will choose some sequence of bits, called s. Next, we will convert each integer into a binary string with a length that is a positive multiple of the length of s (padding the front with zeros if neccessary). Thus, each integer can be considered as a number of groups of binary digits, where each group has a number of bits equal to the length of s. If s is a prefix of any of the binary strings, then our choice of s is invalid (as it may lead to an ambiguous decoding), and we can not use that s.

To transmit the data, we will transmit the groups of the first number, in order from left to right (most significant to least significant). If any of the groups are exactly equal to s, we will transmit those groups twice. After the entire first number is transmitted is this way, we will transmit s once, and then proceed with the next number in the same way. We do not need to transmit s after the last number is sent.

For example, consider transmitting the sequence of numbers {1,3,6,9} with s = "01". First, we convert each number to binary, and pad the left with zeros so that the lengths are each divisible by 2:
{01,11,0110,1001}
Now, when we transmit the data, we have to transmit each group that is equal to s twice:
Number    :    1   |    |  3 |    |    6     |    |    9
Bits sent :  01 01 | 01 | 11 | 01 | 01 01 10 | 01 | 10 01 01
One way to think about this is that s indicates that a new number is about to start, unless it occurs twice in a row.

Different choices of s lead to a different number of bits being sent. Your task is, given a sequence of integers, sequence, find the value of s that requires the fewest bits be transmitted. Return the number of bits that need to be sent for this s.

Definition

Class:
SequenceSerializer
Method:
serialize
Parameters:
int[]
Returns:
int
Method signature:
int serialize(int[] sequence)
(be sure your method is public)

Constraints

  • sequence will contain between 2 and 50 elements, inclusive.
  • Each element of sequence will be greater than or equal to 0.

Examples

  1. {1,3,6,9}
    Returns: 16
    If we set s to "0", we get the sequence: 1 0 11 0 1100 0 100001 Which contains a total of 16 bits.
  2. {1,3,7,15,31,63,127}
    Returns: 34
    The best thing to do here is set s = "0". The bit sequence this gives is: 1 0 11 0 111 0 1111 0 11111 0 111111 0 1111111
  3. {1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000}
    Returns: 204
    The optimal value of s is "011". "11" is not valid because it is equal to some of the first groups of the integers.
  4. {1000000,1000001,1000002,1000003,1000004,
     1000010,1000011,1000012,1000013,1000014,
     1000020,1000021,1000022,1000023,1000024,
     1000030,1000031,1000032,1000033,1000034,
     1000040,1000041,1000042,1000043,1000044,
     1000050,1000051,1000052,1000053,1000054,
     1000060,1000061,1000062,1000063,1000064,
     1000070,1000071,1000072,1000073,1000074,
     1000080,1000081,1000082,1000083,1000084,
     1000090,1000091,1000092,1000093,1000094}
    Returns: 1208
  5. {0,0}
    Returns: 3
Back to all problems