CCO '19 P6 - Bad Codes

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 0.6s
Memory limit: 1G

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
Canadian Computing Olympiad: 2019 Day 2, Problem 3

Your friend has constructed a code that they want to use to send secret messages to you. The messages will only be composed of N different symbols and each symbol will correspond to one binary sequence with at most M bits.

However, you are not sure the code is going to work: there is a chance that a binary sequence can correspond to two (or more) different messages.

For example, if the code was:

\displaystyle \begin{align*}
A &\to 101 \\
B &\to 10 \\
C &\to 1 \\
D &\to 100

then the binary sequence 101 could be correspond to either A or BC.

Your job is determine the length of the shortest binary sequence that corresponds to two different messages, or determine that there are no binary sequences which correspond to two different messages.

Input Specification

The first line of input will contain two space-separated integers N and M (1 \le N, M \le 50). The next N lines of input each will have at least one and at most M characters from the set \{0, 1\}.

For 4 of the 25 marks available, N = 4 and M \le 6.

For an additional 7 of the 25 marks available, each of the binary sequences contains exactly one 1 bit. For example, the sequences 00100 or 1000 would be valid in this case.

Output Specification

Output will be one line long.

If there is a binary sequence that corresponds to two (or more) messages, print the length of the shortest such binary sequence; otherwise, output one line containing -1.

Sample Input 1

4 3

Output for Sample Input 1


Explanation of Output for Sample Input 1

This is the sample in the problem description.

Sample Input 2

4 4

Output for Sample Input 2


Explanation of Output for Sample Input 2

There is no binary sequence that corresponds to more than one message. Notice that since each code is 4 bits, and none are the same, every encoding of 4k bits can be uniquely decoded into k characters.


  • 2
    0rang3  commented on July 29, 2020, 7:48 a.m.

    This code outputs the sample output but cannot solve sample subtasks. I would be grateful if you could help to take a look at this!