#P9694. [GDCPC 2023] New but Nostalgic Problem

[GDCPC 2023] New but Nostalgic Problem

题目描述

Given nn strings w1,w2,,wnw_1, w_2, \cdots, w_n, please select kk strings among them, so that the lexicographic order of string vv is minimized, and output the optimal string vv. String vv satisfies the following constraint: vv is the longest common prefix of two selected strings with different indices. Also, vv is the lexicographically largest string among all strings satisfying the constraint.

More formally, let S\mathbb{S} be a set of size kk, where all the elements in the set are integers between 11 and nn (both inclusive) and there are no duplicated elements. Let lcp(wi,wj)\text{lcp}(w_i, w_j) be the longest common prefix of string wiw_i and wjw_j, please find a set S\mathbb{S} to minimize the lexicographic order of the following string vv and output the optimal string vv.

$$v = \max\limits_{i \in \mathbb{S}, j \in \mathbb{S}, i \ne j} \text{lcp}(w_i, w_j) $$

In the above expression, max\max is calculated by comparing the lexicographic order of strings.

Recall that:

  • String pp is a prefix of string ss, if we can append some number of characters (including zero characters) at the end of pp so that it changes to ss. Specifically, empty string is a prefix of any string.
  • The longest common prefix of string ss and string tt is the longest string pp such that pp is a prefix of both ss and tt. For example, the longest common prefix of abcde and abcef is abc, while the longest common prefix of abcde and bcdef is an empty string.
  • String ss is lexicographically smaller than string tt (sts \ne t), if
    • ss is a prefix of tt, or
    • sp+1<tp+1s_{|p| + 1} < t_{|p| + 1}, where pp is the longest common prefix of ss and tt, p|p| is the length of pp, sis_i is the ii-th character of string ss, and tit_i is the ii-th character of string tt.
  • Specifically, empty string is the string with the smallest lexicographic order.

输入格式

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and kk (2n1062\leq n\leq 10^6, 2kn2\leq k\leq n) indicating the total number of strings and the number of strings to be selected.

For the following nn lines, the ii-th line contains a string wiw_i (1wi1061\leq |w_i|\leq 10^6) consisting of lower-cased English letters.

It's guaranteed that the total length of all strings of all test cases will not exceed 10610^6.

输出格式

For each test case output one line containing one string indicating the answer. Specifically, if the answer is an empty string, print EMPTY\texttt{EMPTY}.

2
5 3
gdcpc
gdcpcpcp
suasua
suas
sususua
3 3
a
b
c
gdcpc
EMPTY