编辑距离既是指给定两个字符串,给定添加,删除,改变3种操作,求定最少的操作次数:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f, N = 1e3 + 5, MOD = 1e9 + 7;
int T, cas = 0;
int n, m;
int dp[N][N];
char s[N], t[N];
int main(){
while(scanf("%s%s",s,t) != EOF) {
int n = strlen(s), m = strlen(t);
for(int i = 0; i <= n; i ++) dp[i][0] = i;
for(int i = 0; i <= m; i ++) dp[0][i] = i;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <=m; j ++) {
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + 1;
dp[i][j] = min(dp[i][j], dp[i-1][j-1] + (s[i-1] != t[j-1]));
}
}
printf("%d\n", dp[n][m]);
}
return 0;
}