#P11089. [ROI 2021] 手机游戏 (Day 1)

[ROI 2021] 手机游戏 (Day 1)

Background

Translated from ROI 2021 D1T2

Problem Description

You are given a sequence of nn crystals arranged from left to right. Each crystal belongs to one of kk types, denoted by the first kk 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 k×kk \times k table AA, whose entries are only 00 and 11. If Ai,j=1A_{i,j} = 1, it means that when the crystal immediately in front of it is of type ii, it is allowed to delete a crystal of type jj. These operations may be performed in any order.

A string xx is lexicographically smaller than a string yy if at least one of the following holds:

  • There exists a position mm that exists in both strings, and before position mm the two strings are identical, but the mm-th character of xx is smaller than the mm-th character of yy.
  • String xx is a strict prefix of string yy (i.e., it can be obtained by deleting one or more characters from the end of yy).

For example, ccf is lexicographically smaller than cff because the 22-nd character of ccf (c) is smaller than the 22-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 kk and nn (1k26,1n5000001 \le k \le 26,1 \le n \le 500000), representing the number of crystal types and the length of the initial crystal sequence.

The next kk lines give the table AA. The ii-th line contains kk characters, each being 0 or 1. The character in row ii and column jj is AijA_{ij}.

The last line is a string of nn lowercase English letters, representing the initial crystal sequence. It is guaranteed that the string contains only the first kk letters (for example, the first seven letters are a, b, c, d, e, f, g). The ii-th letter represents the ii-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 AA. 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 11, you can delete as follows:

In sample 22, you can delete as follows:

  • bcacb\text{bcacb}
  • bcacb\underline{\text{b}}\xcancel{\text{c}}\text{acb}
  • bacb\text{bacb}

Constraints:

Subtask Score nn\le kk\le
11 1010 2020 2626
22 1212 5050 55
33 1616 300300
44 1717 500500 2626
55 1010 20002000
66 99 1000010000
77 88 100000100000
88 1111 500000500000 22
99 77 2626

For 100%100\% of the testdata, n500000n \le 500000 and k26k \le 26

Translated by ChatGPT 5