P18128 [ICPC 2026 Seoul R] Safe

Description

An intelligence agency has obtained a locked safe containing secret documents. Attached to the safe is a sheet of paper containing two sequences of digits, $X$ and $Y$, and $k$ tables of operation costs. An intelligence analyst has discovered that the code to unlock the safe can be determined as follows. Both $X$ and $Y$ consist of digits from $1$ to $9$, inclusive. Each cost table specifies the costs of three operations on a sequence of digits. - **Replacement.** Replace one digit with another digit. - **Insertion.** Insert a digit at any position. - **Deletion.** Delete a digit from any position. Each operation has a nonnegative integer cost. For a fixed cost table, let $\delta(A,B)$ denote the minimum total cost of transforming sequence $A$ into sequence $B$ using any number of these operations, including zero operations. Each cost table is represented by a $10\times 10$ matrix $C$. Its rows and columns are indexed, in order, by $1,2,\ldots,9,0$, where $0$ represents the empty symbol. The entries are interpreted as follows. - For $1\le a,b\le 9$, $C_{a,b}$ is the cost of replacing digit $a$ with digit $b$. - For $1\le a\le 9$, $C_{a,0}$ is the cost of deleting digit $a$. - For $1\le b\le 9$, $C_{0,b}$ is the cost of inserting digit $b$. The costs satisfy the following properties. 1. $C_{a,a}=0$ for every $a\in\{1,2,\ldots,9,0\}$. 2. $C_{a,b}=C_{b,a}$ for every $a,b\in\{1,2,\ldots,9,0\}$. 3. $C_{a,c}\le C_{a,b}+C_{b,c}$ for every $a,b,c\in\{1,2,\ldots,9,0\}$. Now, partition $Y$ into $p\ge 2$ nonempty contiguous subsequences $Y_1,Y_2,\ldots,Y_p$, such that $Y=Y_1Y_2\cdots Y_p$. For a fixed cost table $C$ and a fixed partition, define the partition cost as $\max_{1\le i\le p}\delta(X,Y_i)$. The candidate code for a cost table is the minimum partition cost over all possible choices of $p$ and all valid partitions of $Y$. Finally, the code required to unlock the safe is the minimum candidate code among the $k$ cost tables. For example, suppose $X=12345$, $Y=123412345642345$, and the three cost tables are as shown in the figure below. ![](https://cdn.luogu.com.cn/upload/image_hosting/tg69hwrl.webp) For the first table, partitioning $Y$ into $12341$, $23456$, and $42345$ gives transformation costs $4$, $5$, and $3$, respectively. The minimum possible partition cost for this table is $5$. For the second table, partitioning $Y$ into $1234$, $123456$, and $42345$ gives transformation costs $4$, $4$, and $4$, respectively. The minimum possible partition cost for this table is $4$. For the third table, the same partition gives transformation costs $5$, $4$, and $3$, respectively. The minimum possible partition cost for this table is $5$. Therefore, the code to unlock the safe is $\min\{5,4,5\}=4$. Given the two sequences $X$ and $Y$ and the $k$ cost tables, write a program to determine the code to unlock the safe.

Input Format

Your program is to read from standard input. The first line contains two strings $X$ and $Y$, followed by an integer $k$ ($3\le |X|\le 10$, $2|X|\le |Y|\le 10{,}000$, $2\le k\le 100$). Both strings consist only of digits from $1$ to $9$, inclusive. The next $10k$ lines describe the $k$ cost tables, with $10$ consecutive lines for each table. For each table, the $i$-th line contains $10$ space-separated integers, representing the operation costs in the row corresponding to digit $i$ for $1\le i\le 9$, or to the empty symbol for $i=10$. The columns correspond, in order, to digits $1,2,\ldots,9$, followed by the empty symbol. Each cost is an integer between $0$ and $9$, inclusive. It is guaranteed that every cost table satisfies the properties stated above.

Output Format

Your program is to write to standard output. Print exactly one integer on a single line, giving the code required to unlock the safe.