字符串的编辑距离
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 100000000
using namespace std;
int num[110],sum[110],dp[110][110];
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
string str1[1510];
bool cmp(string a, string b)
{
return a.length() < b.length();
}
int num[1010];
int dp[15][15];
int main(){
int i,j,t,T,p,q,n,m,len1,len2;
char str[20];
scanf("%d",&T);
for(t=1;t<=T;t++){
scanf("%d %d",&n,&m);
for(i=0;i<n;i++){
scanf("%s",str);
str1[i]=str;
}
printf("Case #%d:\n",t);
sort(str1,str1+n, cmp);
for(i=0;i<m;i++){
int ans=0;
scanf("%s %d",str,&num[i]);
len2=strlen(str);
for(j=0;j<n;j++){
len1=str1[j].length();
if(len2-len1>num[i])
continue;
if(len1-len2>num[i])
break;
memset(dp,0,sizeof(dp));
for(p=0;p<=len1;p++)
dp[p][0]=p;
for(p=0;p<=len2;p++)
dp[0][p]=p;
for(p=1;p<=len1;p++)
for(q=1;q<=len2;q++)
dp[p][q]=min(min(dp[p-1][q],dp[p][q-1])+1,dp[p-1][q-1]+(str1[j][p-1]==str[q-1]?0:1));//依次表示删除,插入,替换
if(dp[len1][len2]<=num[i])
ans++;
}
printf("%d\n",ans);
}
}
return 0;
}
本文详细介绍了字符串编辑距离的概念、计算方法及其在不同场景的应用案例,包括文本比较、拼写检查等。通过实例演示了如何使用动态规划解决字符串匹配问题,并探讨了其在实际开发中的价值。
3896

被折叠的 条评论
为什么被折叠?



