#abc268d. D - Unique Username

D - Unique Username

Score : 400400 points

问题描述

高桥在为一个服务选择用户名时遇到了困难。编写一段代码来帮助他。

找出满足以下所有条件的字符串 XX

  • XX 是通过以下步骤获取的:
    • S1,S2,,SNS_1', S_2', \ldots, S_N' 成为 S1,S2,,SNS_1, S_2, \ldots, S_N 的一个排列。按照顺序,令 XXS1S_1'、(1个或多个 _ 符号)、S2S_2'、(1个或多个 _ 符号)、\ldots、(1个或多个 _ 符号)以及 SNS_N' 的连接。
  • 字符串 XX 的长度在 331616 之间(包括两端点)。
  • XX 不与给定的 MM 个字符串 T1,T2,,TMT_1,T_2,\ldots,T_M 中的任何一个相等。

如果不存在满足所有条件的 XX,则输出 -1

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

Takahashi is having trouble with deciding a username for a service. Write a code to help him.

Find a string XX that satisfies all of the following conditions:

  • XX is obtained by the following procedure:
    • Let S1,S2,,SNS_1', S_2', \ldots,S_N' be a permutation of S1,S2,,SNS_1, S_2, \ldots,S_N. Let XX be the concatenation of S1S_1', (11 or more copies of _), S2S_2', (11 or more copies of _), \ldots, (11 or more copies of _), and SNS_N', in this order.
  • The length of XX is between 33 and 1616, inclusive.
  • XX does not coincide with any of MM strings T1,T2,,TMT_1,T_2,\ldots,T_M.

If there is no XX that satisfies all of the conditions, print -1 instead.

Constraints

  • 1N81 \leq N \leq 8
  • 0M1050 \leq M \leq 10^5
  • NN and MM are integers.
  • 1Si161 \leq |S_i| \leq 16
  • N1+Si16N-1+\sum{|S_i|} \leq 16
  • SiSjS_i \neq S_j if iji \neq j.
  • SiS_i is a string consisting of lowercase English letters.
  • 3Ti163 \leq |T_i| \leq 16
  • TiTjT_i \neq T_j if iji \neq j.
  • TiT_i is a string consisting of lowercase English letters and _.

Input

Input is given from Standard Input in the following format:

NN MM

S1S_1

S2S_2

\vdots

SNS_N

T1T_1

T2T_2

\vdots

TMT_M

Output

Print a string XX that satisfies all of the conditions. If there is no XX that satisfies all of the conditions, print -1 instead.
If there are multiple solutions, print any of them.

Sample Input 1

1 1
chokudai
chokudai

Sample Output 1

-1

The only string that satisfies the first and second conditions is X=X= chokudai, but it coincides with T1T_1.
Thus, there is no XX that satisfies all of the conditions, so -1 should be printed.

Sample Input 2

2 2
choku
dai
chokudai
choku_dai

Sample Output 2

dai_choku

Strings like choku__dai (which has two _'s between choku and dai) also satisfy all of the conditions.

Sample Input 3

2 2
chokudai
atcoder
chokudai_atcoder
atcoder_chokudai

Sample Output 3

-1

chokudai__atcoder and atcoder__chokudai (which have two _'s between chokudai and atcoder) have a length of 1717, which violates the second condition.

Sample Input 4

4 4
ab
cd
ef
gh
hoge
fuga
____
_ab_cd_ef_gh_

Sample Output 4

ab__ef___cd_gh

The given TiT_i may contain a string that cannot be obtained by the procedure described in the first condition.

update @ 2024/3/10 11:18:23