This page is still under construction.

Parts of this page are still being built. What you see may change.

Edit Distance

Time limit8sMemory limit128 MB

Summary
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 AA into string BB. An edit script is made of the four commands below, and each command consumes AA from the front while building the result string.

  • Add (a): output one character to the result. AA is left untouched.
  • Delete (d): remove the first character of AA and output nothing.
  • Modify (m): remove the first character of AA and output a different character instead.
  • Copy (c): remove the first character of AA 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 AA and BB, find the minimum number of add, delete, and modify commands (the edit distance) used by a shortest edit script that turns AA into BB.

Input

The first line contains string AA and the second line contains string BB. Both strings consist only of English letters (uppercase and lowercase) and digits, and each has length between 11 and 1700017000, inclusive.

Output

Print a single integer: the minimum number of add, delete, and modify commands needed to turn AA into BB.

Examples4

  1. Example 1

    Input
    abcde
    xabzdey
    
    Expected output
    3
    
  2. Example 2

    Input
    a
    a
    
    Expected output
    0
    
  3. Example 3

    Input
    a
    b
    
    Expected output
    1
    
  4. Example 4

    Input
    kitten
    sitting
    
    Expected output
    3