题解
- 最短编辑距离的应用,我们只需要遍历每个字符串,求每个字符串和输入的字符串的最短距离是否小于等于限制,如果符合答案就+1
代码
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1100;
int n, m;
char str[N][N];
int f[N][N];
int editor(char a[], char b[]) {
int la = strlen(a + 1), lb = strlen(b + 1);
for (int i = 0; i <= lb; i++) f[0][i] = i;
for (int i = 0; i <= la; i++) f[i][0] = i;
for (int i = 1; i <= la; i++) {
for (int j = 1; j <= lb; j++) {
f[i][j] = min(f[i - 1][j] + 1, f[i][j - 1] + 1);
if (a[i] == b[j]) f[i][j] = min(f[i][j], f[i - 1][j - 1]);
else f[i][j] = min(f[i][j], f[i - 1][j - 1] + 1);
}
}
return f[la][lb];
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> str[i] + 1;
while (m--) {
char s[N];
int limit;
cin >> s + 1 >> limit;
int res = 0;
for (int i = 1; i <= n; i++) {
if (editor(s, str[i]) <= limit) {
res++;
}
}
cout << res << endl;
}
return 0;
}