Edit distance on table

시간 제한3초메모리 제한1024 MB

요약
격자 위를 걸으며 만든 문자열과 주어진 문자열 T 사이의 편집 거리를 최소로 만드는 경로를 찾는다.
난이도

보통10점 중 7점

유형
동적 계획법, 그래프, BFS
정답자
아직 제출이 없습니다

문제

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.

HH WW

c_1,1c_1,2…c_1,Wc\_{1,1} c\_{1,2} \dots c\_{1,W}

c_2,1c_2,2…c_2,Wc\_{2,1} c\_{2,2} \dots c\_{2,W}

⋮\vdots

c_H,1c_H,2…c_H,Wc\_{H,1} c\_{H,2} \dots c\_{H,W}

TT

HH and WW (2≤H,W≤1002 \le H, W \le 100) represents the height and the width of the table respectively. c_i,jc\_{i,j} (1≤i≤H1 \le i \le H, 1≤j≤W1 \le j \le 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. c_i,jc\_{i,j} and TT consist of lowercase English letters.

출력

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

예제1

  1. 예제 1

    입력
    2 2
    ab
    ar
    abracadabra
    
    예상 출력
    2