Under construction

Still being built. Things may change

Barcodes

Time limit2sMemory limit1024 MB

Summary
Decode a Code-11 barcode from measured light and dark region widths, check the C and K characters, and print the message or the first error found.
Level

Medium4 of 10

Topics
Implementation, String, Simulation
Solved
No attempts yet

Problem

Code-11 is a barcode system for encoding characters, mainly used to label telecommunications equipment. The encodable characters are the digits 0 through 9, the dash (-), and a special start/stop character that appears at the beginning and end of every Code-11 barcode.

Code-11 is a discrete system, and each character is encoded independently. A character's encoding is five adjacent regions, or bars, that alternate between dark and light, starting with a dark bar. Each bar is narrow or wide according to the table below, where 0 is a narrow bar and 1 is a wide bar.

CharacterEncoding
000001
110001
201001
311000
400101
510100
601100
700011
810010
910000
-00100
Start/Stop00110

The character 1 is encoded as a wide dark bar, a narrow light bar, a narrow dark bar, a narrow light bar, and a wide dark bar. Each character's barcode is followed by a narrow light bar that exists only to separate characters.

A two-width encoding is simple. The reader only has to tell narrow bars from wide bars, so Code-11 tolerates a lot of print variation on low-quality printing.

To detect errors, every Code-11 barcode here has two check characters, C and K, added at the end of the message (before the stop character). If the n message characters are c_1 through c_n from left to right, the weight of C is

(∑i=1n((n−i) mod 10+1)×w(ci)) mod 11\left(\sum_{i=1}^{n}\left((n - i) \bmod 10 + 1\right) \times w(c_i)\right) \bmod 11

where w(c_i) is the weight of c_i. Digits 0 through 9 weigh 0 through 9, and the hyphen weighs 10. (mod binds more tightly than +.)

The weight of K is

(∑i=1n+1((n−i+1) mod 9+1)×w(ci)) mod 11\left(\sum_{i=1}^{n+1}\left((n - i + 1) \bmod 9 + 1\right) \times w(c_i)\right) \bmod 11

where c_{n+1} is the C check character. For example, for the message 123-45, C is 5 and K is 2. The barcodes for the eight characters 123-4552, preceded and followed by the start/stop barcode, form the complete Code-11 barcode.

Simple barcode readers measure the light reflected from a barcode with a linear array of hundreds of small CCD sensors. Each sensor reports light or dark. The software identifies the light and dark regions, uses their widths to validate the barcode, and extracts the encoded data. The orientation of the barcode is not fixed, so the software must decode a barcode scanned from left to right as well as from right to left.

The task is to decode the widths of the light and dark regions detected by the reader. A wide bar is intended to be twice as wide as a narrow bar. Because printers are inconsistent, each bar can be up to 5 percent wider or narrower than intended. No message has zero length.

Input

The input contains several test cases, each representing one scan by the barcode reader. Each case begins with an integer m≤150m \le 150, the number of regions detected. It is followed by mm integers d1…dmd_1 \ldots d_m with 1≤di≤2001 \le d_i \le 200, the number of sensors in each region. All sensors in a region report the same light intensity. Each case begins and ends with a dark bar, with no leading or trailing white space.

The last test case is followed by a single integer 0.

Output

For each input case, print a line with the case number and the decoding result. If the barcode decodes successfully, print the message characters without the check characters. If decoding succeeds but the C check character is wrong, print bad C. If C is correct but the K check character is wrong, print bad K. Print bad code if the barcode cannot be decoded because bar widths are outside the allowed range, the start/stop codes are missing or invalid, or some other condition is invalid. Follow the format of the sample output.

Examples1

  1. Example 1

    Input
    59
    10 20 20 10 10 10 20 10 10 20
    10 10 10 10 20 10 20 10 10 10
    20 10 20 10 20 10 20 10 10 10
    10 10 20 10 10 10 10 10 10 20
    20 10 20 10 10 20 10 10 20 10
    10 10 20 10 10 20 20 10 10
    
    35
    10 10 10 10 10 10 10 10 10 10
    10 10 10 10 10 10 10 10 10 10
    10 10 10 10 10 10 10 10 10 10
    10 10 10 10 10
    
    35
    10 10 20 20 10 10 20 10 10 10
    20 10 10 20 10 10 20 10 10 10
    20 10 20 10 20 10 10 10 10 10
    10 10 20 20 10
    
    0
    
    Expected output
    Case 1: 123-45
    Case 2: bad code
    Case 3: bad K