codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

2174A.md (2477B)


      1 Source: https://codeforces.com/problemset/problem/2174/A
      2 
      3 A. Needle in a Haystack
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You got lucky to know the answer to all important questions in the world. This time, the answer is the string $s$, consisting of lowercase English letters only. You want to hide this string.
     22 
     23 You have another string $t$ also consisting of lowercase English letters only. You need to shuffle the letters in $t$ so that the string $s$ appears at least once in $t$ as a subsequence$^{\text{∗}}$. Among all possible reorderings of $t$ containing $s$ as a subsequence, find the lexicographically smallest$^{\text{†}}$ one.
     24 
     25 $^{\text{∗}}$A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) element from arbitrary positions.
     26 
     27 $^{\text{†}}$A string $a$ is lexicographically smaller than string $b$ if and only if one of the following holds:
     28 
     29 * $a$ is a prefix of $b$, but $a \ne b$; or
     30 * in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$.
     31 
     32 Input
     33 
     34 Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 10^4$). The description of the test cases follows.
     35 
     36 The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$), where $|s|$ is the length of the string $s$.
     37 
     38 The second line of each test case contains the string $t$ ($|s| \le |t| \le 10^5$).
     39 
     40 Both strings consist of lowercase English letters only.
     41 
     42 The sum of $|t|$ over all test cases does not exceed $10^5$.
     43 
     44 Output
     45 
     46 For each test case, print a single string: the lexicographically smallest reordering of letters in the string $t$ that contains $s$ as a subsequence. If no such string exists, print "Impossible" instead.
     47 
     48 Example
     49 
     50 Input
     51 
     52 ```
     53 3
     54 
     55 dcbe
     56 
     57 bedbaecfc
     58 
     59 babadab
     60 
     61 abacabadabacaba
     62 
     63 babaisyou
     64 
     65 flagiswin
     66 ```
     67 
     68 Output
     69 
     70 ```
     71 abcdcbeef
     72 
     73 aaaaabababccdab
     74 
     75 Impossible
     76 ```
     77 
     78 Note
     79 
     80 In the first test case, $\mathtt{abc}\,\mathtt{dcbe}\,\mathtt{ef}$ contains $\mathtt{dcbe}$.
     81 
     82 In the second test case, $\mathtt{aaaaa}\,\mathtt{baba}\,\mathtt{bcc}\,\mathtt{dab}$ also contains $\mathtt{babadab}$.
     83 
     84 It can be proven that these are the lexicographically smallest strings satisfying the given condition.
     85 
     86 In the third test case, no rearrangement of letters in $t$ contains $s$.