算法1
(DP)
C++ 代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1010,INF=1e9;
char a[N],b[N];
int f[N][N];
int main(){
cin>>a+1>>b+1;
int n = strlen(a+1);
int m = strlen(b+1);
//初始化
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
f[i][j] = INF;
}
}
for(int i=0;i<=n;i++) f[i][0] = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
f[i][j] = f[i-1][j];
if(a[i]==b[j]) f[i][j] = min(f[i][j],f[i-1][j-1]); //修改
else{ //不用修改
f[i][j] = min(f[i][j],f[i-1][j-1]+1);
}
}
}
cout<<f[n][m]<<endl;
return 0;
}