当前位置 : 主页 > 编程语言 > java >

编辑距离

来源:互联网 收集:自由互联 发布时间:2022-09-02
#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 ,


#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;
}


上一篇:swpu&&fjut2016级寒假训练三题解
下一篇:没有了
网友评论