CF2180B.Ashmal

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You have an array aa of nn strings a1,a2,…,ana_{1}, a_{2}, \ldots, a_{n}, each consisting of lowercase English letters, and an empty string ss.

In the ii-th (1≤i≤n1 \le i \le n) step, you should do one of the following:

  • add aia_{i} to the beginning of ss, or
  • add aia_{i} to the end of ss.

For example, if before the ii-th step s=abas = \mathtt{aba} and ai=bbaa_{i} = \mathtt{bba}, after the ii-th step, ss will be equal to ababba\mathtt{ababba} or bbaaba\mathtt{bbaaba}.

What's the lexicographically smallest string ss you can reach after nn steps?

A string aa is lexicographically smaller than a string bb of the same length, if and only if the following holds:

  • in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb.

你有一个由 nn 个字符串 a1,a2,…,ana_{1}, a_{2}, \ldots, a_{n} 构成的数组 aa,每个字符串均由小写英文字母组成,以及一个空字符串 ss。

在第 ii 步(1≤i≤n1 \le i \le n)中,你需要执行以下操作之一:

  • 将 aia_{i} 添加到 ss 的开头,或
  • 将 aia_{i} 添加到 ss 的末尾。

例如,若在第 ii 步之前 s=abas = \mathtt{aba} 且 ai=bbaa_{i} = \mathtt{bba},则第 ii 步之后,ss 将变为 ababba\mathtt{ababba} 或 bbaaba\mathtt{bbaaba}。

经过 nn 步后,你能得到的字典序最小的字符串 ss 是什么?

当且仅当满足以下条件时,字符串 aa 的字典序小于长度相同的字符串 bb:

  • 在 aa 与 bb 首次出现不同字符的位置上,aa 中该位置的字母在字母表中早于 bb 中对应位置的字母。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤10001 \le n \le 1000) — size of array aa. The next line contains nn strings a1,a2,…,ana_{1}, a_{2}, \ldots, a_{n} (1≤∣ai∣≤40001 \le |a_i| \le 4000), each consisting of lowercase English letters.

It is guaranteed that the sum of nn over all test cases does not exceed 10001000, and the total length of all strings in the input (over all test cases) does not exceed 40004000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5001 \le t \le 500)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤10001 \le n \le 1000)—— 数组 aa 的大小。下一行包含 nn 个字符串 a1,a2,…,ana_{1}, a_{2}, \ldots, a_{n}(1≤∣ai∣≤40001 \le |a_i| \le 4000),每个字符串均由小写英文字母组成。

保证所有测试用例的 nn 之和不超过 10001000,且输入中所有字符串的总长度(所有测试用例之和)不超过 40004000。

输出格式

For each test case, print the lexicographically minimum string ss you can reach after nn steps.

对于每个测试用例,输出经过 nn 步后所能到达的字典序最小的字符串 ss。

输入输出样例

  • 输入#1

    3
    4
    amir rima amin nima
    1
    codeforces
    3
    a ab abc

    输出#1

    aminamirrimanima
    codeforces
    aababc

说明/提示

In the first test case, one possible way to construct the lexicographically minimum string ss is as follows:

  1. After the first step, s=amirs = \mathtt{amir} regardless of whether we add it to the beginning or the end, since ss was initially empty.
  2. In the second step, we add a2=rimaa_2 = \mathtt{rima} to the end of ss. Now s=amirrimas = \mathtt{amirrima}.
  3. In the third step, we add a3=amina_3 = \mathtt{amin} to the beginning of ss. Now s=aminamirrimas = \mathtt{aminamirrima}.
  4. In the last step, we add a4=nimaa_4 = \mathtt{nima} to the end of ss. Therefore, the final value of ss is aminamirrimanima\mathtt{aminamirrimanima}.

It can be proven that this resulting string is indeed the lexicographically smallest string obtainable after all steps.

在第一个测试用例中,构造字典序最小的字符串 ss 的一种可能方式如下:

  1. 第一步后,无论将字符串添加到开头还是结尾,均有 s=amirs = \mathtt{amir},因为初始时 ss 为空。
  2. 第二步中,我们将 a2=rimaa_2 = \mathtt{rima} 添加到 ss 的末尾。此时 s=amirrimas = \mathtt{amirrima}。
  3. 第三步中,我们将 a3=amina_3 = \mathtt{amin} 添加到 ss 的开头。此时 s=aminamirrimas = \mathtt{aminamirrima}。
  4. 最后一步中,我们将 a4=nimaa_4 = \mathtt{nima} 添加到 ss 的末尾。因此,ss 的最终值为 aminamirrimanima\mathtt{aminamirrimanima}。

可以证明,该结果字符串确实是所有操作完成后所能得到的字典序最小的字符串。

输入解题思路,AI测评打分。不知道怎么写?

首页