//求最小编辑距离
int editDistance(const string source, const string target)
{
//step 1
int len_source = source.length();
int len_target = target.length();
if (len_source == 0) return len_target;
if (len_target == 0) return len_source;
//Construct a matrix
typedef vector< vector<int> > Tmatrix;
Tmatrix matrix(len_target + 1);
for (int i = 0; i <= len_target; i++) matrix[i].resize(len_source + 1);
//step 2 Initialize
for (int i = 1; i <= len_target; i++) matrix[i][0] = i;
for (int i = 1; i <= len_source; i++) matrix[0][i] = i;
//step 3
for (int i = 1; i <= len_target; i++)
{
const char si = source[i - 1];
//step 4
for (int j = 1; j <= len_source; j++)
{
const char tj = target[j - 1];
//step 5
int cost = si==tj ? 0:1;
//step 6
const int ins = matrix[i - 1][j] + 1;
const int del = matrix[i][j - 1] + 1;
const int replace = matrix[i - 1][j - 1] + cost;
matrix[i][j] = min(ins, min(del, replace));
}
}//step7
return matrix[len_target][len_source];
}