【样例输入】 fxpimu xwrs 【样例输出】 5 【样例说明】 【评分标准】 #include<iostream> #include<string> #include <malloc.h> using namespace std; int min_val(const int a,const int b,const int c) { int min = a<b?a:b; min = min<c?min:c; return min; } int edit_distance(const string &str1,const string &str2) { int m=(int)str1.length(),n=(int)str2.length(),i,j,cost; if(m==0)return n; if(n==0)return m; int **dp=(int **)malloc((m+1)*sizeof(int*)); for(i=0; i<=m; ++i)dp[i]=(int *)malloc((n+1)*sizeof(int)); for(i=0; i<=m; ++i)dp[i][0] = i; for(j=1; j<=n; ++j)dp[0][j] = j; for(i=1; i<=m; ++i) for(j=1; j<=n; ++j) { cost = (str1[i-1]==str2[j-1]?0:1); dp[i][j] = min_val(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+cost); } cost = dp[m][n]; for(i=0; i<=m; ++i)delete dp[i]; delete dp; return cost; } int main() { string str1,str2; cin>>str1>>str2; cout<<edit_distance(str1,str2)<<endl; }