Magic Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1404 Accepted Submission(s): 589
Problem Description
There are many magic numbers whose lengths are less than 10. Given some queries, each contains a single number, if the Levenshtein distance (see below) between the number in the query and a magic number is no more than a threshold, we call the magic number is the lucky number for that query. Could you find out how many luck numbers are there for each query?
Levenshtein distance (from Wikipedia http://en.wikipedia.org/wiki/Levenshtein_distance):
In information theory and computer science, the Levenshtein distance is a string metric for measuring the amount of difference between two sequences. The term edit distance is often used to refer specifically to Levenshtein distance.
The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character. It is named after Vladimir Levenshtein, who considered this distance in 1965.
For example, the Levenshtein distance between "kitten" and "sitting" is 3, since the following three edits change one into the other, and there is no way to do it with fewer than three edits:
1.kitten → sitten (substitution of 's' for 'k')
2.sitten → sittin (substitution of 'i' for 'e')
3.sittin → sitting (insertion of 'g' at the end).
Input
There are several test cases. The first line contains a single number T shows that there are T cases. For each test case, there are 2 numbers in the first line: n (n <= 1500) m (m <= 1000) where n is the number of magic numbers and m is the number of queries.
In the next n lines, each line has a magic number. You can assume that each magic number is distinctive.
In the next m lines, each line has a query and a threshold. The length of each query is no more than 10 and the threshold is no more than 3.
Output
For each test case, the first line is "Case #id:", where id is the case number. Then output m lines. For each line, there is a number shows the answer of the corresponding query.
Sample Input
1 5 2 656 67 9313 1178 38 87 1 9509 1
Sample Output
Case #1: 1 0
if( abs( len1-len2 ) >k ) continue ;
if( abs( s[p].size()-s1.size() ) >k ) continue ;这是错的,经验以后尽量不要函数套函数
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n,m,k,ans;
string s[1506];
string s1;
int dp[16][16];
void solve()
{
int i,j,p;
int len2=s1.size();
ans=0;
for(p=1; p<=n; p++)
{
int len1=s[p].size();
if( abs( len1-len2 ) >k ) continue ;
memset(dp,0,sizeof(dp));
for(i=0;i<=s1.size();i++)
{
dp[0][i]=i;
}
for(j=0;j<=s[p].size();j++)
{
dp[j][0]=j;
}
for(i=1; i<=len1; i++)
{
for(j=1; j<=len2 ; j++)
{
if( s[p][i-1] == s1[j-1] )
dp[i][j]=dp[i-1][j-1];
else
{
dp[i][j]=min( dp[i][j-1],min( dp[i-1][j],dp[i-1][j-1] ) ) + 1;
}
}
}
if( dp[ len1 ][ len2 ] <= k ) ans++;
}
}
int main()
{
int i,t,test=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=1; i<=n; i++)
{
cin>>s[i];
}
printf("Case #%d:\n",++test);
while(m--)
{
cin>>s1>>k;
solve();
printf("%d\n",ans);
}
}
return 0;
}
基于Levenshtein距离的魔法数字查询问题

博客围绕长度小于10的魔法数字展开,给定查询,若查询数字与魔法数字的Levenshtein距离不超过阈值,则该魔法数字为幸运数。介绍了Levenshtein距离的定义及示例,还给出输入输出格式和样例,最后提及代码中函数套函数的错误经验。
5万+

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



