Magic Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1404 Accepted Submission(s): 589Problem 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.
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
题意: 给出一个字典,然后m个询问,每次给出一个字符串和一个k,让你找出字典中与该字符串最短编辑距离小于k的有多少个。(两字符串最短编辑距离:一个字符串经过插入、删除、替换最少需几步变为另一个字符串)
思路: 本体复杂度我没有降下来,906ms擦线过了 = =,就是每次枚举字典然后dp求最短编辑距离就够了。 dp初始化要记得dp[0][j]、dp[i][0]都是有意义的,不是INF,开始WA了。 s1、s2两字符串。 dp[i][j] i-s1的位置 j-s2的位置 dp-最少操作几次能将前面的变为一样 if(s1[i]==s2[j]) dp[i][j]=min(dp[i-1][j-1],dp[i-1][j]+1,dp[i][j-1]+1); else dp[i][j]=min(dp[i-1][j-1],dp[i][j-1],dp[i-1][j])+1;
代码:
#include#include #include #include #include #include #include