Under construction

Still being built. Things may change

Names

Interview

Time limit2sMemory limit1024 MB

Summary
Given two strings, find the lexicographically largest string that is a subsequence of both, or print nothing if none exists.
Level

Hard8 of 10

Topics
String, Greedy, Dynamic programming
Solved
No attempts yet

Problem

On the distant planet Tau Ceti there are customs we find hard to understand. For example, the Tau Cetians choose names for their children in a way very unlike Earthlings. Parents choose a name for their child so that it can be obtained both by deleting some set of letters from the father's name and by deleting some set of letters from the mother's name. For example, if the father is named "abacaba" and the mother is named "bbccaa", their child can have the names "a", "bba", "bcaa", but cannot have the names "aaa", "ab", or "bbc". The child's name may coincide with the father's or the mother's name if it can be obtained from the other parent's name by deleting several (possibly zero) letters.

Suppose a father named X and a mother named Y are choosing a name for their newborn child. At Tau Cetian schools students are often called to the board in lexicographic order of their names, that is, in the order the names appear in a dictionary, so the parents want to choose a name for their child that comes as late as possible lexicographically.

Formally, a string S is lexicographically greater than a string T if one of the following two conditions holds:

  • the string T is obtained from S by deleting one or more letters from the end of S;
  • the first (i - 1) characters of T and S do not differ, and the letter at position i of T comes earlier in the alphabet than the letter at position i of S.

Write a program that, given the names of the father and the mother, finds the lexicographically largest name for their child.

Input

The first line of the input file contains X, the father's name. The second line contains Y, the mother's name. Each name consists of lowercase letters of the Latin alphabet, contains at least one letter, and has length at most 105 letters.

Output

The output file must contain the lexicographically largest possible name for the child. If no suitable name exists for the child, the output file must be empty.

Note

In the first example the child's name cannot start with a letter greater than c, since the father's name contains no such letters. The letter c is contained in both names, so the child's name can start with it. The only letter that can follow c in the child's name is a.

Examples2

  1. Example 1

    Input
    abcabca
    abcda
    
    Expected output
    ca
    
  2. Example 2

    Input
    ccba
    accbbaa
    
    Expected output
    ccba