题面
You have an array of strings , each consisting of lowercase English letters, and an empty string .
In the -th () step, you should do one of the following:
- add to the beginning of , or
- add to the end of .
For example, if before the -th step and , after the -th step, will be equal to or .
What’s the lexicographically smallest string you can reach after steps?
A string is lexicographically smaller than a string of the same length, if and only if the following holds:
- in the first position where and differ, the string has a letter that appears earlier in the alphabet than the corresponding letter in .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains a single integer () — size of array . The next line contains strings (), each consisting of lowercase English letters.
It is guaranteed that the sum of over all test cases does not exceed , and the total length of all strings in the input (over all test cases) does not exceed .
Output
For each test case, print the lexicographically minimum string you can reach after steps.
思路
设处理到第 步后的当前字符串为 ,本步只有两种选择 或 ,目标是使最终字符串字典序最小
关键性质 若 (字典序),则对任意固定字符串 有 且 ,即同前缀或同后缀拼接保持字典序
因此对固定 ,函数 关于 单调不减
令 为前 个字符串操作后可达的字典序最小结果,有递推:
NOTE证明
用归纳,任取上一步可达的任意 ,都有 ,由单调性得 ,故本步从最小状态转移得到的即为全局最小
算法 初始化 ,依次读入 并令 ,输出最终
复杂度 设该测试用例总字符数为 ,每步构造并比较两次拼接,整体可视为 ,在约束 下足够