Magic Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1867 Accepted Submission(s): 763
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
题目大意:给你t组数据。每组有n个数字串,有m个询问,每个询问有一个数字串和一个次数阀值,问你将n个数字串转化成询问数字串的编辑次数不大于阀值的有多少个。编辑距离是指:将一个串通过增加删除替换变成另外的串的最少次数。
解题思路:想过kmp,感觉不太靠谱,想到判断最长公共子序列,写到一半没再写了。后来看别人说可以直接dp,然后发现其实这个dp跟最长公共子序列的dp比较像。这里定义dp[i][j]为s串的前i个字符跟t串的前j个字符形成的编辑距离。
如果s[i]!=s[j] dp[i][j]=min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])。
如果s[i]==s[j] dp[i][j]=dp[i-1][j-1]。
同时注意初始化应该让dp[i][0]=i。dp[0][j]=i。表示跟空串进行转化的编辑距离。
#include<bits/stdc++.h> using namespace std; #define min(a,b) ((a)<(b)?(a):(b)) char Map[1600][15], str[15]; int len[1600]; int dp[20][20]; int main(){ int t,cnt=0,n,m; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%s",Map[i]); len[i]=strlen(Map[i]); } printf("Case #%d: ",++cnt); int lim; while(m--){ scanf("%s%d",str,&lim); int lens=strlen(str); int sum=0; for(int k=1;k<=n;k++){ if(abs(len[k]-lens)<=lim){ memset(dp,0,sizeof(dp)); for(int i=1;i<=lens;i++) dp[i][0]=i; for(int j=1;j<=len[k];j++) dp[0][j]=j; for(int i=1;i<=lens;i++){ for(int j=1;j<=len[k];j++){ if(str[i-1]==Map[k][j-1]){ dp[i][j]=dp[i-1][j-1]; }else{ dp[i][j]=min(min(dp[i-1][j-1],dp[i][j-1]),dp[i-1][j])+1; } } } if(dp[lens][len[k]]<=lim) sum++; } } printf("%d ",sum); } } return 0; }