Edit distance on table
시간 제한3초메모리 제한1024 MB
격자 위를 걸으며 만든 문자열과 주어진 문자열 T 사이의 편집 거리를 최소로 만드는 경로를 찾는다.
문제
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.
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.