#P15727. [JAG 2023 Summer Camp #3] Edit distance on table

[JAG 2023 Summer Camp #3] Edit distance on table

题目描述

You have a table with HH rows and WW 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 SS be a string of length 11 containing the letter in the cell.
  • Step 2: Do either
    • stop building SS, or
    • select a cell from four cells which shares an edge with the current one. Then, append the letter in the cell to SS, and move to the cell. Then, repeat step 2.

You also have a string TT. Your mission is to minimize the edit distance between SS and TT.

The edit distance (also known as Levenshtein distance) between string UU and VV is the minimum number of steps required to convert UU into VV by using the following operations.

  • Replace a character in UU with another one.
  • Insert a character into UU.
  • Delete a character from UU.

输入格式

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} $$

HH and WW (2H,W1002 \leq H, W \leq 100) represents the height and the width of the table respectively. ci,jc_{i,j} (1iH1 \leq i \leq H, 1jW1 \leq j \leq W) is a character in the cell in the ii-th row and the jj-th column. TT is a non-empty string. The length of TT doesn't exceed 2,0002,000. ci,jc_{i,j} and TT consist of lowercase English letters.

输出格式

Output the minimum possible edit distance between SS and TT in one line.

2 2
ab
ar
abracadabra
2