TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

# Decompress Binary Messages Using Directed Graphs

## When you're faced with a number of possible solutions to a problem, determining the optimal one is a critical task. Follow a practical example for optimization tasks using stored procedures and graphs.

 by Alex Kozak
 Nov 16, 2006
 Page 1 of 5
ertain programming tasks require determining the optimal (or best) solution from many possible solutions. Usually this kind of task can be illustrated and described with graphs—mathematical structures that help model relations between objects in a set. This article demonstrates a practical approach to some optimization tasks. As an example, it uses a problem from the ACM International Collegiate Programming Contest (ACM-ICPC) 2006 Finals.

The Problem
The example ACM-ICPC 2006 World Finals problem states that the following is one possible method to compress any binary message:

Replace any maximal sequence of n 1's with the binary version of n whenever it shortens the length of the message.

For example, the compressed form of the data 11111111001001111111111111110011 becomes 10000010011110011. The original data is 32 bits long while the compressed data is only 17 bits long.

The drawback of this method is that sometimes the decompression process yields more than one result for the original message, making it impossible to obtain the original message.

Write a program that determines if the original message can be obtained from the compressed data when the length of the original message (L), the number of 1's in the original message (N), and the compressed data are given. The original message will be no longer than 16 Kbytes and the compressed data will be no longer than 40 bits.

Input
The input file contains several test cases. Each test case has two lines. The first line contains L and N, and the second line contains the compressed data.

Output
For each test case, output a line containing the case number (starting with 1) and a message: YES, NO, or NOT UNIQUE. YES means that the original message can be obtained. NO means that the compressed data has been corrupted and the original message cannot be obtained. NOT UNIQUE means that more than one message could have been the original message.

``````
Sample Input			Output for the Sample Input
------------------------------------------------------------------------
32 26 				Case #1: YES
10000010011110011
9 7 				Case #2: NOT UNIQUE
1010101
14 14 				Case #3: NO
111111
``````

Analyzing the problem reveals the following:

1. The binary number n, which represents n 1's in the compressed message, will be separated by zero(s) from any binary number m, which represents m 1's. For example, in the sample input 10000010011110011, you can't have 10000010011 (104310) and 110011 (5110). If you did, you would replace 1043 1's (part of the sequence of 1's), leaving two more 1's uncompressed. Since the problem requires replacing the maximal sequence of 1's, this separation would be incorrect.
2. The binary number n that represents n 1's cannot be less than 310 or more than 13107210. Indeed, the replacement of one or two 1's won't shorten the original message. The actual compression can start only from three 1's, when 1112 in the original message will be replaced by 112. The upper limit for the binary number n follows from the fact that the original message can be no longer than 16 Kbytes = 16 * 1024 * 8 = 13107210 bits. That also means the maximal length of n cannot exceed 17 bits (217 = 13107210), even though the length of the compressed message can be up to 40 bits.