QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 2048 MB 總分: 25

#2723. 坏代码

统计

你的朋友构建了一套编码,想要用它来向你发送秘密消息。这些消息仅由 $N$ 个不同的符号组成,每个符号对应一个长度不超过 $M$ 位的二进制序列。

然而,你不确定这套编码是否有效:因为存在一种可能,即同一个二进制序列可以对应两个(或更多)不同的消息。

例如,如果编码如下:

$A \to 101$ $B \to 10$ $C \to 1$ $D \to 100$

那么二进制序列 $101$ 既可以对应 $A$,也可以对应 $BC$。

你的任务是确定对应两个不同消息的最短二进制序列的长度,或者确定不存在对应两个不同消息的二进制序列。

输入格式

输入的第一行包含两个空格分隔的整数 $N$ 和 $M$ ($1 \le N, M \le 50$)。接下来的 $N$ 行输入,每行包含至少 $1$ 个且至多 $M$ 个来自集合 $\{0, 1\}$ 的字符。

在总共 $25$ 分的测试点中,有 $4$ 分满足 $N = 4$ 且 $M \le 6$。

在另外 $7$ 分的测试点中,每个二进制序列恰好包含一个 $1$ 位。例如,$00100$ 或 $1000$ 在这种情况下是有效的。

输出格式

输出仅占一行。

如果存在一个对应两个(或更多)消息的二进制序列,请输出该最短二进制序列的长度;否则,输出一行 $-1$。

样例

输入 1

4 3
101
10
1
100

输出 1

3

说明 1

这是题目描述中的样例。

输入 2

4 4
1011
1000
1111
1001

输出 2

-1

说明 2

不存在对应多于一个消息的二进制序列。注意,由于每个编码都是 $4$ 位,且互不相同,因此每一个 $4k$ 位的编码都可以唯一地解码为 $k$ 个字符。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.