P2758 编辑距离

#include<bits/stdc++.h>
using namespace std;
const int n=2010;
char a[n],b[n];
int dp[n][n];

int main(){
    scanf("%s%s",a+1,b+1);
    int l1 = strlen(a+1);
    int l2 = strlen(b+1);
    for(int i=1;i<=l1;i++)dp[i][0]=i;
    for(int i=1;i<=l2;i++)dp[0][i]=i;
    for(int i=1;i<=l1;i++){
        for(int j=1;j<=l2;j++){
            if(a[i]==b[j])dp[i][j]=dp[i-1][j-1];
            else dp[i][j]=min(dp[i][j-1],min(dp[i-1][j],dp[i-1][j-1]))+1;
        }
    }
    printf("%d
",dp[l1][l2]);
    return 0;
}
原文地址:https://www.cnblogs.com/LightyaChoo/p/13184348.html