#K90972. Find and Replace Pattern

    ID: 37871 Type: Default 1000ms 256MiB

Find and Replace Pattern

Find and Replace Pattern

You are given a list of words and a pattern string. A word matches the pattern if and only if there exists a bijection between letters in the pattern and letters in the word such that replacing each letter in the pattern with the corresponding letter in the bijection yields the word.

In other words, for a word w and a pattern p, if you encode both as sequences of integers representing the first occurrence of each character (i.e. if for every index i, we assign the value $a_i$, where $a_i$ is the index of the first occurrence of that character in the string), then w matches p if their encoded sequences are equal.

For example, for pattern abb its encoded form is $[0,1,1]$. Only words with the same encoding will be a match.

inputFormat

The input is read from standard input (stdin) with the following format:

The first line contains an integer n denoting the number of words.
The second line contains n space-separated words.
The third line contains a single pattern string.

If n = 0, the second line will be empty.

outputFormat

Output to standard output (stdout) a single line containing the words from the list that match the given pattern, in their original order, separated by a single space. If no word matches the pattern, output an empty line.

## sample
6
abc deq mee aqq dkd ccc
abb
mee aqq