【51nod-1183】编辑距离
链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1183
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
char s1[N], s2[N];
int dp[N][N];
int main()
{
cin>>s1>>s2;
int l1 = strlen(s1);
int l2 = strlen(s2);
for(int i = 0; i < l1; i++) dp[i+1][0] = i+1;
for(int i = 0; i < l2; i++) dp[0][i+1] = i+1;
for(int i = 0; i < l1; i++)
for(int j = 0; j < l2; j++)
{
if(s1[i] == s2[j]) dp[i+1][j+1] = dp[i][j];
else dp[i+1][j+1] = min(dp[i][j], min(dp[i+1][j], dp[i][j+1]))+1;
}
printf("%d\n", dp[l1][l2]);
return 0;
}