编辑距离
核心
1. 计算复杂度
2. 线性dp + n * m 次查询
#include <iostream>
#include <cstring>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 15, M = 1010;
int f[N][N];
char s[M][N];
int check(char *a, char *b) {
int na = strlen(a + 1), nb = strlen(b + 1);
//printf("%s %s\n", a + 1, b + 1);
for (int i = 0; i <= na; i++) f[i][0] = i;
for (int i = 0; i <= nb; i++) f[0][i] = i;
for (int i = 1; i <= na; i++) {
for (int j = 1; j <= nb; j++) {
f[i][j] = min(f[i - 1][j] + 1, f[i][j - 1] + 1);
f[i][j] = min(f[i][j], f[i - 1][j - 1] + (a[i] != b[j]));
}
}
return f[na][nb];
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", s[i] + 1);
for (int i = 0; i < m; i++) {
int limit;
char a[N];
scanf("%s%d", a + 1, &limit);
int res = 0;
for (int j = 0; j < n; j++) {
if (check(s[j], a) <= limit) res++;
}
printf("%d\n", res);
}
return 0;
}