아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Password

시간 제한1.6초메모리 제한1024 MB

요약
B의 문자를 같은 개수만큼 사용하고 A의 부분열인 문자열 가운데 사전순으로 가장 앞선 것을 찾아 출력한다.
난이도

보통10점 중 6점

유형
그리디, 문자열, 누적 합
정답자
아직 제출이 없습니다

문제

Due to his paranoia about security, Sam decided to pick a long password for his e-mail account that contains only small letters of the English alphabet. However, he realized that he had a high risk of forgetting his password, so he decided to encode it into 2 strings, also containing only small letters of the English alphabet. He wrote these strings on a piece of paper and hid the paper under his bed. Sam chose the strings A and B such that the password S is the anagram of B that appears as a subsequence of A and is lexicographically minimal.

We say that a string B = b1b2...b|B| is a subsequence of A = a1a2...a|A| if and only if there exists a sequence of strictly increasing indices n1, n2, ..., n|B| such that ani=bi for i = 1, 2, ..., |B|.

We say that A is lexicographically smaller than B if and only if there exists an index n < |A| such that ai=bi for i = 1, 2, ..., n and an+1 < bn+1.

We say that S is an anagram of B if S and B contain the same letters and in the same quantities, but possibly in different orders.

As expected, Sam forgot his password one week later and now he is struggling to get it back, so he asks for your help. Write a program that can find his password for him.

Given the two strings A and B, print out Sam's password based on the restrictions above.

입력

The input has exactly one line containing the 2 strings A and B separated by one whitespace.

출력

The output should contain one line representing Sam's password. In case there is no solution, you should print out the word “impossible” (without the quotation marks).

예제3

  1. 예제 1

    입력
    abacaba bab
    
    예상 출력
    abb
    
  2. 예제 2

    입력
    abacaba cbc
    
    예상 출력
    impossible
    
  3. 예제 3

    입력
    abacaba caab
    
    예상 출력
    aacb