#P15727. [JAG 2023 Summer Camp #3] Edit distance on table
[JAG 2023 Summer Camp #3] Edit distance on table
题目描述
You have a table with rows and columns. Each cell of the table contains a letter.
You are going to construct a string by the following steps.
- Step 1: Pick up a cell in the table and let be a string of length containing the letter in the cell.
- Step 2: Do either
- stop building , or
- select a cell from four cells which shares an edge with the current one. Then, append the letter in the cell to , and move to the cell. Then, repeat step 2.
You also have a string . Your mission is to minimize the edit distance between and .
The edit distance (also known as Levenshtein distance) between string and is the minimum number of steps required to convert into by using the following operations.
- Replace a character in with another one.
- Insert a character into .
- Delete a character from .
输入格式
The input consists of a single test case in the following format.
$$\begin{aligned} &H \ W \\ &c_{1,1} c_{1,2} \ldots c_{1,W} \\ &c_{2,1} c_{2,2} \ldots c_{2,W} \\ &\vdots \\ &c_{H,1} c_{H,2} \ldots c_{H,W} \\ &T \end{aligned} $$and () represents the height and the width of the table respectively. (, ) is a character in the cell in the -th row and the -th column. is a non-empty string. The length of doesn't exceed . and consist of lowercase English letters.
输出格式
Output the minimum possible edit distance between and in one line.
2 2
ab
ar
abracadabra
2