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).

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 wikipedia

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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;
}