CF2174A.Needle in a Haystack

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You got lucky to know the answer to all important questions in the world. This time, the answer is the string ss, consisting of lowercase English letters only. You want to hide this string.

You have another string tt also consisting of lowercase English letters only. You need to shuffle the letters in tt so that the string ss appears at least once in tt as a subsequence∗^{\text{∗}}. Among all possible reorderings of tt containing ss as a subsequence, find the lexicographically smallest†^{\text{†}} one.

∗^{\text{∗}}A sequence aa is a subsequence of a sequence bb if aa can be obtained from bb by the deletion of several (possibly, zero or all) element from arbitrary positions.

†^{\text{†}}A string aa is lexicographically smaller than string bb if and only if one of the following holds:

  • aa is a prefix of bb, but a≠ba \ne b; or
  • 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.

你很幸运地知道了世界上所有重要问题的答案。这一次,答案是字符串 ss,它仅由小写英文字母组成。你想将这个字符串隐藏起来。

你还有另一个字符串 tt,它也仅由小写英文字母组成。你需要对 tt 中的字母进行重排,使得字符串 ss 至少作为 tt 的一个子序列∗^{\text{∗}} 出现。在所有满足“ss 是其子序列”的 tt 的重排中,请找出字典序最小†^{\text{†}} 的那个。

∗^{\text{∗}} 序列 aa 是序列 bb 的子序列,当且仅当 aa 可通过从 bb 中任意位置删除若干(可能为零个或全部)元素而得到。

†^{\text{†}} 字符串 aa 的字典序小于字符串 bb,当且仅当以下条件之一成立:

  • aa 是 bb 的真前缀(即 aa 是 bb 的前缀,但 a≠ba \ne b);或者
  • 在 aa 与 bb 首次出现不同字符的位置上,aa 中该位置的字母在字母表中早于 bb 中对应位置的字母。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases TT (1≤T≤1041 \le T \le 10^4). The description of the test cases follows.

The first line of each test case contains the string ss (1≤∣s∣≤1051 \le |s| \le 10^5), where ∣s∣|s| is the length of the string ss.

The second line of each test case contains the string tt (∣s∣≤∣t∣≤105|s| \le |t| \le 10^5).

Both strings consist of lowercase English letters only.

The sum of ∣t∣|t| over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 TT(1≤T≤1041 \le T \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含字符串 ss(1≤∣s∣≤1051 \le |s| \le 10^5),其中 ∣s∣|s| 表示字符串 ss 的长度。

每个测试用例的第二行包含字符串 tt(∣s∣≤∣t∣≤105|s| \le |t| \le 10^5)。

两个字符串均由小写英文字母组成。

所有测试用例中 ∣t∣|t| 的总和不超过 10510^5。

输出格式

For each test case, print a single string: the lexicographically smallest reordering of letters in the string tt that contains ss as a subsequence. If no such string exists, print "Impossible" instead.

对于每个测试用例,输出一个字符串:字符串 tt 的字母的字典序最小重排,使得该重排包含 ss 作为子序列。如果不存在这样的字符串,则输出 "Impossible"。

输入输出样例

  • 输入#1

    3
    dcbe
    bedbaecfc
    babadab
    abacabadabacaba
    babaisyou
    flagiswin

    输出#1

    abcdcbeef
    aaaaabababccdab
    Impossible

说明/提示

In the first test case, abc dcbe ef\mathtt{abc}\,\mathtt{dcbe}\,\mathtt{ef} contains dcbe\mathtt{dcbe}.

In the second test case, aaaaa baba bcc dab\mathtt{aaaaa}\,\mathtt{baba}\,\mathtt{bcc}\,\mathtt{dab} also contains babadab\mathtt{babadab}.

It can be proven that these are the lexicographically smallest strings satisfying the given condition.

In the third test case, no rearrangement of letters in tt contains ss.

在第一个测试用例中,abc dcbe ef\mathtt{abc}\,\mathtt{dcbe}\,\mathtt{ef} 包含 dcbe\mathtt{dcbe}。

在第二个测试用例中,aaaaa baba bcc dab\mathtt{aaaaa}\,\mathtt{baba}\,\mathtt{bcc}\,\mathtt{dab} 也包含 babadab\mathtt{babadab}。

可以证明,这些是满足给定条件的字典序最小的字符串。

在第三个测试用例中,tt 中字母的任意重排均不包含 ss。

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

首页