#abc219c. C - Neo-lexicographic Ordering

C - Neo-lexicographic Ordering

Score : 300300 points

问题描述

Takahashi,治理AtCoder王国的人,已决定改变英语小写字母的字母顺序。

新的字母顺序由字符串XX表示,该字符串是a, b, \ldots, z的一个排列。XX的第ii个字符(1i261 \leq i \leq 26)将是新顺序中第ii小的英文字母。

王国中有NN位公民,其名字分别为S1,S2,,SNS_1, S_2, \dots, S_N,其中每个SiS_i (1iN)(1 \leq i \leq N)由小写英文字母组成。
按照Takahashi决定的字母顺序,对这些名字进行字典序排序。

什么是字典序?

简单来说,字典序就是词典中单词排列的顺序。更正式地讲,以下是确定不同字符串SSTT之间的字典序的算法。

以下,用SiS_i表示字符串SS的第ii个字符。此外,如果SS在字典序上小于TT,我们记作S<TS \lt T;如果SS在字典序上大于TT,我们记作S>TS \gt T

  1. LLSSTT长度中较小的那个。对于每个i=1,2,,Li=1,2,\dots,L,检查SiS_iTiT_i是否相同。
  2. 如果存在某个ii使得SiTiS_i \neq T_i,令jj为最小的这样的ii。然后比较SjS_jTjT_j。若SjS_j在字母顺序上早于TjT_j,则确定S<TS \lt T并停止比较;若SjS_j晚于TjT_j,则确定S>TS \gt T并停止比较。
  3. 如果不存在满足SiTiS_i \neq T_iii,则比较SSTT的长度。若SSTT短,则确定S<TS \lt T并停止比较;若SSTT长,则确定S>TS \gt T并停止比较。

按照Takahashi决定的新字母顺序,请按字典序对这NN位公民的名字进行排序。

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

Problem Statement

Takahashi, who governs the Kingdom of AtCoder, has decided to change the alphabetical order of English lowercase letters.

The new alphabetical order is represented by a string XX, which is a permutation of a, b, \ldots, z. The ii-th character of XX (1i26)(1 \leq i \leq 26) would be the ii-th smallest English lowercase letter in the new order.

The kingdom has NN citizens, whose names are S1,S2,,SNS_1, S_2, \dots, S_N, where each SiS_i (1iN)(1 \leq i \leq N) consists of lowercase English letters.
Sort these names lexicographically according to the alphabetical order decided by Takahashi.

What is the lexicographical order?

Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings SS and TT.

Below, let SiS_i denote the ii-th character of SS. Also, if SS is lexicographically smaller than TT, we will denote that fact as S<TS \lt T; if SS is lexicographically larger than TT, we will denote that fact as S>TS \gt T.

  1. Let LL be the smaller of the lengths of SS and TT. For each i=1,2,,Li=1,2,\dots,L, we check whether SiS_i and TiT_i are the same.
  2. If there is an ii such that SiTiS_i \neq T_i, let jj be the smallest such ii. Then, we compare SjS_j and TjT_j. If SjS_j comes earlier than TjT_j in alphabetical order, we determine that S<TS \lt T and quit; if SjS_j comes later than TjT_j, we determine that S>TS \gt T and quit.
  3. If there is no ii such that SiTiS_i \neq T_i, we compare the lengths of SS and TT. If SS is shorter than TT, we determine that S<TS \lt T and quit; if SS is longer than TT, we determine that S>TS \gt T and quit.

Constraints

  • XX is a permutation of a, b, \ldots, z.
  • 2N500002 \leq N \leq 50000
  • NN is an integer.
  • 1Si10(1iN)1 \leq |S_i| \leq 10 \, (1 \leq i \leq N)
  • SiS_i consists of lowercase English letters. (1iN)(1 \leq i \leq N)
  • SiSjS_i \neq S_j (1i<jN)(1 \leq i \lt j \leq N)

Input

Input is given from Standard Input in the following format:

XX

NN

S1S_1

S2S_2

\vdots

SNS_N

Output

Print NN lines. The ii-th line (1iN)(1 \leq i \leq N) should contain the ii-th smallest name when the citizens' names are sorted according to the alphabetical order decided by Takahashi.

Sample Input 1

bacdefghijklmnopqrstuvwxzy
4
abx
bzz
bzy
caa

Sample Output 1

bzz
bzy
abx
caa

In the new alphabetical order set by Takahashi, b is smaller than a and z is smaller than y. Thus, sorting the citizens' names lexicographically would result in bzz, bzy, abx, caa in ascending order.

Sample Input 2

zyxwvutsrqponmlkjihgfedcba
5
a
ab
abc
ac
b

Sample Output 2

b
a
ac
ab
abc

update @ 2024/3/10 09:41:18

}