AcWing 902. 最短编辑距离
原题链接
简单
作者:
minux
,
2020-05-03 14:40:02
,
所有人可见
,
阅读 494
#include <bits/stdc++.h>
using namespace std;
const int N=1005;
int f[N][N];
int n, m;
int main(){
string a, b;
cin>>n;
cin>>a;
cin>>m;
cin>>b;
memset(f, 0x00, sizeof f);
for(register int i=1; i<=m; ++i) f[0][i]=i; // only insert for string a
for(register int j=1; j<=n; ++j) f[j][0]=j; // only delete for string a
for(int i=1; i<=n; ++i)
for(int j=1; j<=m; ++j){
// op: Delete & Update & Insert
f[i][j]=min(f[i-1][j], min(f[i][j-1], f[i-1][j-1]))+1;
if(a[i-1]==b[j-1]) f[i][j]=min(f[i][j], f[i-1][j-1]);
}
cout<<f[n][m]<<endl;
return 0;
}