Magic Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 866 Accepted Submission(s): 353
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).
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.
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
参考wikipedia
int LevenshteinDistance(char s[1..m], char t[1..n])
{
// for all i and j, d[i,j] will hold the Levenshtein distance between
// the first i characters of s and the first j characters of t;
// note that d has (m+1)x(n+1) values
declare int d[0..m, 0..n]
clear all elements in d // set each element to zero
// source prefixes can be transformed into empty string by
// dropping all characters
for i from 1 to m
{
d[i, 0] := i
}
// target prefixes can be reached from empty source prefix
// by inserting every characters
for j from 1 to n
{
d[0, j] := j
}
for j from 1 to n
{
for i from 1 to m
{
if s[i] = t[j] then
d[i, j] := d[i-1, j-1] // no operation required
else
d[i, j] := minimum
(
d[i-1, j] + 1, // a deletion
d[i, j-1] + 1, // an insertion
d[i-1, j-1] + 1 // a substitution
)
}
}
return d[m,n]
}#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char mg[1510][12];
char qu[1010][12];
int th[1010];
int n, m;
int mgl[1510];
int qul[1010];
int ans[1010];
int levenDistance(char a[], char b[]){
int d[12][12];
int i, j;
int la = strlen(a);
int lb = strlen(b);
memset(d, 0, sizeof(d));
for(i = 0; i <= la; i ++)
d[i][0] = i;
for(j = 0; j <= lb; j ++)
d[0][j] = j;
for(j = 1; j <= lb; j ++)
for(i = 1; i <= la; i ++){
if(a[i] == b[j]){
d[i][j] = d[i-1][j-1];
}
else{
d[i][j] = min(d[i-1][j]+1, min(d[i][j-1]+1, d[i-1][j-1]+1));
}
}
//printf("%s %s\n", a, b);
//printf("%d\n", d[la][lb]);
return d[la][lb];
}
int main(){
int t;
int i, j;
int cnt = 0;
scanf("%d", &t);
while(t --){
cnt ++;
char ss[12];
memset(ans, 0, sizeof(ans));
scanf("%d %d", &n, &m);
for(i = 0; i < n; i ++){
scanf("%s", ss);
mg[i][0] = 's';
mg[i][1] = '\0';
strcat(mg[i], ss);
mgl[i] = strlen(mg[i]);
}
for(i = 0; i < m; i ++){
scanf("%s", ss);
qu[i][0] = 's';
qu[i][1] = '\0';
strcat(qu[i], ss);
qul[i] = strlen(qu[i]);
scanf("%d", &th[i]);
}
for(i = 0; i < m; i ++){
for(j = 0; j < n; j ++){
if(abs(qul[i] - mgl[j]) <= th[i]){
if(levenDistance(mg[j], qu[i]) <= th[i])
ans[i] ++;
}
}
}
printf("Case #%d:\n", cnt);
for(i = 0; i < m; i ++)
printf("%d\n", ans[i]);
}
return 0;
}
本文介绍了一种利用Levenshtein距离算法解决特定查询问题的方法,该算法用于衡量两个字符串之间的相似度,通过计算最少编辑距离来找出查询字符串与预设魔法数字间的最相近匹配。
762

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



