Alice in Foxland
Time limit8sMemory limit512 MB
Given two strings X and Y, find the longest common subsequence of X and Y that contains a required substring C as a contiguous block, or report impossible; among ties pick the lexicographically smallest.
- Level
Medium7 of 10
- Topics
- Dynamic programming, String, Greedy, Implementation
- Solved
- No attempts yet
Problem
Defoxyrenardnucleic acids (DNAs for short) and renardnucleic acids (RNAs) are similar organic molecules found in living things. Each molecule is a sequence of 26 kinds of nucleobase, so a sequence is written as a string in which every lowercase letter from a to z denotes one kind of nucleobase. DNAs and RNAs differ only in the way the nucleobases are bonded.
In 2323, Prof. Fuchs discovered that a DNA containing a particular substring acts as a catalyst, that is, it accelerates some chemical reaction. He also discovered that a longer DNA accelerates the reaction more. For example, a DNA containing fox catalyzes the dissolution of nitrogen oxides (NOx). redfox and cutefoxes are two such DNAs, and cutefoxes gives more acceleration than redfox. The DNA foooox is not such a catalyst, because it does not contain fox as a substring.
DNAs are easy to obtain by extraction from plants such as glory lilies. Almost every extracted molecule has a different sequence, so very few of them act as catalysts. Many scientists have worked on a way to obtain the molecules that are wanted.
In 2369, Prof. Hu discovered the following process.
- Prepare two DNAs X and Y.
- Generate RNAs X' and Y' whose sequences are copied from the DNAs X and Y.
- Delete zero or more bases from X' using some chemicals to obtain an RNA X''.
- Also delete zero or more bases from Y' to obtain an RNA Y'' that has the same sequence as X''.
- Obtain a DNA Z whose sequence is the same as the sequences of X'' and Y''.

Figure 1: New method for generation of DNAs
The point is the use of RNAs. Deleting specific bases is difficult on a DNA and relatively easy on an RNA. RNAs are less stable than DNAs, and no way to obtain RNAs directly from an organism is known, which is why RNAs are obtained from DNAs.
Alice is a researcher at the Tail Environmental Natural Catalyst Organization. She is asked to generate DNAs containing a particular substring by applying Hu's method to pairs of DNAs. A longer DNA is preferred, so she wants a way to know the longest DNA that is possible. She asked you for help.
Write a program that outputs the sequence of the longest DNA that can be generated from the given pair of DNAs X and Y and that contains the given substring C.
Input
The input consists of multiple datasets. Each dataset consists of three lines. The first and the second line contain the sequences X and Y. The third line contains the substring C. Each sequence and each substring contains only lowercase letters from a to z, and its length is between 1 and 1600 inclusive.
A line containing a single * follows the last dataset and terminates the input.
Output
For each dataset, output the longest sequence on one line. If two or more longest sequences are possible, output the lexicographically smallest one among them. If a DNA containing C cannot be obtained in any way, output Impossible instead.
For two strings s and t of the same length, s is lexicographically smaller than t when s has the smaller letter at the first position where the two strings differ.