计算字符串距离
同样也是字符串距离计算问题,参考一本通 1276:【例9.20】编辑距离
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
//Mystery_Sky
//
#define INF 0x3f3f3f3f
#define M 3000
int f[M][M];
int len_a, len_b;
char a[M], b[M];
int main() {
int t;
scanf("%d", &t);
getchar();
while(t--) {
memset(f, 0, sizeof(f));
scanf("%s", a);
getchar();
scanf("%s", b);
len_a = strlen(a);
len_b = strlen(b);
for(int i = 1; i <= len_a; i++) f[i][0] = i;
for(int i = 1; i <= len_b; i++) f[0][i] = i;
for(int i = 1; i <= len_a; i++) {
for(int j = 1; j <= len_b; j++) {
if(a[i-1] == b[j-1]) {
f[i][j] = f[i-1][j-1];
}
else f[i][j] = min(min(f[i-1][j], f[i][j-1]), f[i-1][j-1]) + 1;
}
}
printf("%d
", f[len_a][len_b]);
}
return 0;
}