Barcodes
Time limit2sMemory limit1024 MB
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.
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
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
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 , the number of regions detected. It is followed by integers with , 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.