Problem B
Protein
Languages
en
sv
Nikolaj is a chemist and is trying to create a protein with a specific shape. Right now, he has a protein that consists of a chain of amino acids of length $N$. The chain is a string $s$, where each amino acid is a letter. The letters used are the first $K$ letters of the alphabet ($1 \leq K \leq 26$).
Nikolaj can make an amino acid $s_i$ react with $s_{i+1}$ so that $s_{i+1}$ disappears from the chain. But this is only possible for certain pairs of letters. Nikolaj has a $K \times K$ table $A$ with zeros and ones that indicate which pairs of letters can react with each other. If $A_{xy} = 1$, it means that the $x$-th letter of the alphabet can react with the $y$-th letter. So, if for example $A_{23} = 1$ and the string is bccbca, then the first letter can react with the second so that the string becomes bcbca. Then two more reactions can occur so that the string becomes bba.
Nikolaj wants to create a protein that is as small as possible lexicographically by removing letters from $s$ in this way. Your task is to find the lexicographically smallest string possible.
Input
The first line contains two integers $K$ and $N$ ($1 \leq K \leq 26$, $1 \leq N \leq 5 \cdot 10^5$).
Next, there are $K$ lines each containing a string of length $K$, the matrix $A_{ij}$.
The last line contains the string $s$.
Output
Print a string, the lexicographically smallest string that can be created by making deletions as described in the problem.
Scoring
Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.
|
Group |
Points |
Constraints |
|
$1$ |
$15$ |
$N \leq 20$ |
|
$2$ |
$10$ |
$N \leq 300$, $K \leq 5$ |
|
$3$ |
$20$ |
$N \leq 500$ |
|
$4$ |
$10$ |
$N \leq 2000$ |
|
$5$ |
$25$ |
$N \leq 10^5$ |
|
$6$ |
$10$ |
$K \leq 2$ |
|
$7$ |
$10$ |
No additional constraints. |
| Sample Input 1 | Sample Output 1 |
|---|---|
3 7 010 001 100 abacaba |
aac |
| Sample Input 2 | Sample Output 2 |
|---|---|
3 5 010 001 100 bcacb |
bacb |