#P9694. [GDCPC2023] New but Nostalgic Problem
[GDCPC2023] New but Nostalgic Problem
题目描述
Given strings , please select strings among them, so that the lexicographic order of string is minimized, and output the optimal string . String satisfies the following constraint: is the longest common prefix of two selected strings with different indices. Also, is the lexicographically largest string among all strings satisfying the constraint.
More formally, let be a set of size , where all the elements in the set are integers between and (both inclusive) and there are no duplicated elements. Let be the longest common prefix of string and , please find a set to minimize the lexicographic order of the following string and output the optimal string .
$$v = \max\limits_{i \in \mathbb{S}, j \in \mathbb{S}, i \ne j} \text{lcp}(w_i, w_j) $$In the above expression, is calculated by comparing the lexicographic order of strings.
Recall that:
- String is a prefix of string , if we can append some number of characters (including zero characters) at the end of so that it changes to . Specifically, empty string is a prefix of any string.
- The longest common prefix of string and string is the longest string such that is a prefix of both and . For example, the longest common prefix of
abcde
andabcef
isabc
, while the longest common prefix ofabcde
andbcdef
is an empty string. - String is lexicographically smaller than string (), if
- is a prefix of , or
- , where is the longest common prefix of and , is the length of , is the -th character of string , and is the -th character of string .
- 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 indicating the number of test cases. For each test case:
The first line contains two integers and (, ) indicating the total number of strings and the number of strings to be selected.
For the following lines, the -th line contains a string () consisting of lower-cased English letters.
It's guaranteed that the total length of all strings of all test cases will not exceed .
输出格式
For each test case output one line containing one string indicating the answer. Specifically, if the answer is an empty string, print .
题目大意
【题目描述】
给定 个字符串 ,请选出恰好 个字符串,最小化字符串 的字典序,并输出这个最优的字符串 。其中 满足以下条件: 是被选出的字符串中,某两个编号不同的字符串的最长公共前缀。而且, 是所有满足条件的字符串中,字典序最大的字符串。
更正式地,令 表示一个大小为 的集合,集合中的元素均为从 到 的整数(含两端),且没有重复的元素。令 表示字符串 和 的最长公共前缀,您需要找到一个集合 以最小化下述字符串 的字典序,并输出这个最优的字符串 。
$$v = \max\limits_{i \in \mathbb{S}, j \in \mathbb{S}, i \ne j} \text{lcp}(w_i, w_j) $$上式中的 通过字典序比较两个字符串。
请回忆:
- 称字符串 是字符串 的前缀,若可以在 的末尾添加若干个字符(包括零个字符)将它变成 。特别地,空字符串是任意字符串的前缀。
- 字符串 和 的最长公共前缀是一个最长的字符串 ,满足 既是 的前缀,又是 的前缀。例如,
abcde
与abcef
的最长公共前缀为abc
,而abcde
与bcdef
的最长公共前缀为空字符串。 - 称字符串 的字典序小于字符串 (),若
- 是 的前缀,或
- ,其中 为 和 的最长公共前缀, 为 的长度, 表示字符串 的第 个字符, 表示字符串 的第 个字符。
- 特别地,空字符串是字典序最小的字符串。
【输入格式】
有多组测试数据。第一行输入一个整数 表示测试数据组数。对于每组测试数据:
第一行输入两个整数 和 (,),表示字符串的总数和需要选择的字符串的数量。
对于接下来 行,第 行输入一个由小写字母构成的字符串 ()。
保证所有数据中字符串长度之和不超过 。
【输出格式】
每组数据输出一行一个字符串表示答案。特别地,若答案为空字符串,输出 。
2
5 3
gdcpc
gdcpcpcp
suasua
suas
sususua
3 3
a
b
c
gdcpc
EMPTY