Problem:
Edit Distance (Levenshtein Distance)

Find the edit distance between given two words s1 and s2.

The disntace is the minimum number of single-character edits required to change one word into the other. The edits including the following operations:

insertion: Insert a character at a particular position.

deletion: Delete a character at a particular position.

substitution: Change the character at a particular position to a different character

Input

s1
s2

Two words s1 and s2 are given in the first line and the second line respectively. The words will consist of lower case characters.

Output

Print the edit distance in a line.

Constraints

1 ≤ length of s1 ≤ 1000

1 ≤ length of s2 ≤ 1000

Sample Input 1

acac
acm

Sample Output 1

2

Sample Input 2

icpc
icpc

Sample Output 2

0

Solution in C++11 (judge verdict: Accepted)

#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

const int INF = (int)1e9;

int dp[1001][1001];
string A, B;

int solve(int i, int j) {
  if(i == A.length() || j == B.length()) return max(A.length() - i, B.length() - j);
  int &res = dp[i][j];
  if(res != -1) return res;
  if(A[i] == B[j]) return res = solve(i + 1, j + 1);
  res = INF;
  res = min(res, 1 + solve(i + 1, j)); // jの位置にA[i]を挿入
  res = min(res, 1 + solve(i + 1, j + 1)); // B[j]をA[i]に置換
  res = min(res, 1 + solve(i, j + 1)); // jの位置の文字を削除
  return res;
}

int main() {
  cin >> A >> B;
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0) << endl;
}