P27124 [AtCoder] Search
提高-
题目描述

Problem Description

Takahashi often uses a search engine called Yafoo.

Yafoo has $ N $ sites registered, and the registration name of the $ i $ -th site is $ S_i $. When a search is performed with the search word $ T $, among the $ N $ registered sites, all sites whose registration name length is at least $ |T| $ and whose first $ |T| $ characters match $ T $ are obtained as search results.

Now, Takahashi wants the search results to include the sites numbered $A_1,\ A_2,\ ...,\ A_K$. Determine if there exists a search word such that exactly these sites are obtained as results. If such a search word exists, find the shortest one.

Input Format

The input is given from the standard input in the following format:

$ N $ $ K $ $ A_1 $ $ A_2 $ $ ... $ $ A_K $ $ S_1 $ $ S_2 $ $ : $ $ S_N $

Output Format

Output the shortest string that Takahashi should use as the search word. If no such string exists, output -1 instead.

Sample Input and Output

Sample Input #1

3 2
1 2
abc
ab
a

Sample Output #1

ab

Sample Input #2

3 2
1 2
abc
ab
abc

Sample Output #2

-1

Sample Input #3

3 3
1 2 3
abc
ab
abc

Sample Output #3


Notes/Hints

Constraints

  • $1\ ≦\ N\ ≦\ 10^5$
  • $1\ ≦\ K\ ≦\ N$
  • $1\ ≦\ A_i\ ≦\ N$
  • Each $ A_i $ is distinct.
  • $1\ ≦\ |S_i|\ ≦\ 10^5$
  • The total sum of $ |S_i| $ is at most $ 10^5 $.
  • Each $ S_i $ consists of lowercase English letters.

Sample Explanation 1

The string ab is a search word that satisfies the conditions.

Sample Explanation 3

Note that an empty string can also be a search word.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 2 1 2 abc ab a
样例输出
ab