编辑距离,经典的了。动态规划枚举即过。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
char bgn[1505][15];
char tmp[15];
int dp[15];
int main(){
int T,n,m,kase=0,e,cnt,pre,tpre;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
getchar();
for(int i=1;i<=n;i++){
gets(bgn[i]);
}
printf("Case #%d:\n",++kase);
for(int c=1;c<=m;c++){
cnt=0;
scanf("%s",tmp);
scanf("%d",&e);
for(int i=1;i<=n;i++){
dp[0]=0;
int sb=strlen(bgn[i]);
int st=strlen(tmp);
for(int p=1;p<=sb;p++)
dp[p]=p;
for(int p=1;p<=sb;p++){
pre=dp[0];
dp[0]=p;
for(int k=1;k<=st;k++){
tpre=dp[k];
dp[k]=min(pre+(bgn[i][p-1]==tmp[k-1]?0:1),min(dp[k]+1,dp[k-1]+1));
pre=tpre;
}
}
if(dp[strlen(tmp)]<=e)
cnt++;
}
printf("%d\n",cnt);
}
}
return 0;
}