QOJ.ac

QOJ

时间限制: 2.0 s 内存限制: 2048 MB 总分: 100

#10601. 单词游戏

统计

在“单词游戏”节目中,Ashley 选出了 $n$ 个单词,并要求 Brandon 将它们合并。如果单词 $s$ 的长度为 $k > 0$ 的后缀同时也是单词 $t$ 的前缀,那么这两个单词 $s$ 和 $t$ 就可以合并。合并的结果是一个新单词,由 $s$ 与 $t$ 的后 $|t| - k$ 个字母拼接而成。如果存在多个合法的 $k$ 值,可以任选其一。

Brandon 必须不断地从列表中取出一对可以合并的单词,并将它们替换为合并后的新单词,直到列表中只剩下一个单词。他需要使最终得到的单词长度尽可能短。如果存在多个长度相同的最终单词,Brandon 必须找出字典序最小的那一个。

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 5$),表示初始单词的数量。 接下来的 $n$ 行,每行包含一个长度不超过 5 的小写字母单词。

输出格式

输出 Brandon 能得到的长度最短且字典序最小的单词。如果无法合并成一个单词,输出 $-1$。

样例

样例输入 1

2
aba
bab

样例输出 1

abab

样例输入 2

3
ab
bc
ca

样例输出 2

abca

样例输入 3

2
x
y

样例输出 3

-1

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.