Edit distance on table
Time limit3sMemory limit1024 MB
Given a grid of letters and a string T, find the smallest edit distance between T and any string spelled by a walk on the grid.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Graph, String matching
- Solved
- No attempts yet
Problem
A table has rows and columns. Each cell contains one lowercase English letter.
Build a string with the following steps.
- Pick a cell and let be a string of length 1 containing the letter in that cell.
- Choose one of the following.
- Stop building .
- Choose one of the four cells that share an edge with the current cell. Append its letter to , move to that cell, and repeat step 2.
You are given a string . Your goal is to minimize the edit distance between and .
The edit distance between strings and is the minimum number of operations needed to turn into . The allowed operations are:
- Replace one character with another.
- Insert one character.
- Delete one character.
Input
The first line contains and . Each of the next lines describes one row of the table. The last line contains the string . is not empty and its length is at most . The letters in the table and are all lowercase English letters.
Output
Print the minimum possible edit distance between and on one line.