#P11089. [ROI 2021] 手机游戏 (Day 1)
[ROI 2021] 手机游戏 (Day 1)
Background
Translated from ROI 2021 D1T2。
Problem Description
You are given a sequence of crystals arranged from left to right. Each crystal belongs to one of types, denoted by the first lowercase English letters. Therefore, the crystal sequence can be represented as a string of lowercase letters.
In one move of the game, you may remove one crystal from the sequence. The player's goal is to obtain the lexicographically smallest string using the allowed deletions.
The allowed deletions are defined by a table , whose entries are only and . If , it means that when the crystal immediately in front of it is of type , it is allowed to delete a crystal of type . These operations may be performed in any order.
A string is lexicographically smaller than a string if at least one of the following holds:
- There exists a position that exists in both strings, and before position the two strings are identical, but the -th character of is smaller than the -th character of .
- String is a strict prefix of string (i.e., it can be obtained by deleting one or more characters from the end of ).
For example, ccf is lexicographically smaller than cff because the -nd character of ccf (c) is smaller than the -nd character of cff (f). Also, noi is lexicographically smaller than noip because noi is a strict prefix of noip.
Input Format
The first line contains two integers and (), representing the number of crystal types and the length of the initial crystal sequence.
The next lines give the table . The -th line contains characters, each being 0 or 1. The character in row and column is .
The last line is a string of lowercase English letters, representing the initial crystal sequence. It is guaranteed that the string contains only the first letters (for example, the first seven letters are a, b, c, d, e, f, g). The -th letter represents the -th type of crystal.
Output Format
Output the lexicographically smallest string that can be obtained through the operations.
3 7
010
001
100
abacaba
aac
3 5
010
001
100
bcacb
bacb
Hint
Sample Explanation:
The two samples have the same . They both allow only the following three types of deletions (the underlined character is the previous character, and the struck-out character is the deleted one): $\underline{\text{a}}\xcancel{\text{b}},\underline{\text{b}}\xcancel{\text{c}},\underline{\text{c}}\xcancel{\text{a}}$.
In sample , you can delete as follows:

In sample , you can delete as follows:
Constraints:
| Subtask | Score | ||
|---|---|---|---|
For of the testdata, and 。
Translated by ChatGPT 5