Edit Distance
Time limit8sMemory limit128 MB
Given strings A and B up to length 17000, find the minimum number of insert, delete, and substitute operations to turn A into B.
- Level
Medium7 of 10
- Topics
- Dynamic programming, String
- Solved
- No attempts yet
Problem
Consider an edit script that turns string into string . An edit script is made of the four commands below, and each command consumes from the front while building the result string.
- Add (
a): output one character to the result. is left untouched. - Delete (
d): remove the first character of and output nothing. - Modify (
m): remove the first character of and output a different character instead. - Copy (
c): remove the first character of and output that same character.
Copy is free. The length of an edit script is the number of add, delete, and modify commands it uses, and the shortest edit script is the one that minimizes the count of these three commands.
Given two strings and , find the minimum number of add, delete, and modify commands (the edit distance) used by a shortest edit script that turns into .
Input
The first line contains string and the second line contains string . Both strings consist only of English letters (uppercase and lowercase) and digits, and each has length between and , inclusive.
Output
Print a single integer: the minimum number of add, delete, and modify commands needed to turn into .