#P17101. [ICPC 2017 Qingdao R] Suffix

    ID: 18808 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2017后缀自动机 SAM哈希 hashingICPC青岛

[ICPC 2017 Qingdao R] Suffix

Problem Description

Consider nn given non-empty strings denoted by s1,s2,,sns_1, s_2, \cdots, s_n. Now for each of them, you need to select a corresponding suffix, denoted by suf1,suf2,,sufn\text{suf}_1, \text{suf}_2, \cdots, \text{suf}_n. For each string sis_i, the suffix sufi\text{suf}_i is a non-empty substring whose right endpoint is the endpoint of the entire string. For instance, all suffixes of the string “jiangsu” are “u”, “su”, “gsu”, “ngsu”, “angsu”, “iangsu” and itself.

All selected suffixes could assemble into a long string $T = \text{suf}_1 + \text{suf}_2 + \cdots + \text{suf}_n$. Here plus signs indicate additions of strings placing the latter at the tail of the former. Your selections of suffixes would determine the lexicographical order of TT. Now, your mission is to find the one with minimum lexicographical order.

Here is a hint about lexicographical order. To compare strings of different lengths, the shorter string is usually padded at the end with enough “blanks” which is a special symbol that is treated as smaller than every letters.

Input Format

The first line of input contains an integer TT which is the total number of test cases. For each case, the first line contains an positive integer nn. Each of the following nn lines contains a string entirely in lowercase, corresponding to s1,s2,,sns_1, s_2, \cdots, s_n. The summation of lengths of all strings in input is smaller or equal to 500000.

Output Format

For each test case, output the string TT with minimum lexicographical order.

3
3
bbb
aaa
ccc
3
aba
aab
bab
2
abababbaabbababba
abbabbabbbababbab
baaac
aaabab
aab