Magic Number(Levenshtein distance算法)
2015-08-26 21:25
127 查看
Magic Number
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 4323
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
Levenshtein distance算法
s[i] == t[j] :dp[i][j] = min (dp[i-1][j-1] , min(dp[i-1][j] , dp[i][j-1]) + 1) ;
s[i] != t[j]: dp[i][j] = min (dp[i-1][j-1] , dp[i-1][j] , dp[i][j-1]) + 1 ;
其实就是个dp.
这种复杂度为O(n*m) ;
还有一种复杂度为O(n*k),k为最大改变次数,n为字符串长度。
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 4323
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
Levenshtein distance算法
s[i] == t[j] :dp[i][j] = min (dp[i-1][j-1] , min(dp[i-1][j] , dp[i][j-1]) + 1) ;
s[i] != t[j]: dp[i][j] = min (dp[i-1][j-1] , dp[i-1][j] , dp[i][j-1]) + 1 ;
其实就是个dp.
这种复杂度为O(n*m) ;
还有一种复杂度为O(n*k),k为最大改变次数,n为字符串长度。
相关文章推荐
- 三道题 2015.8.26(二进制中1的个数,数组中出现次数超过一半的数字,最大(小)的K个数)
- poj 3685 Matrix 二分里套二分
- 21 Merge Two Sorted Lists
- cf Div#317 Lengthening Sticks
- C++中虚继承派生类构造函数的正确写法
- xcode快捷键
- 跨域资源共享(CORS)安全性浅析[内有提及OPTIONS请求发起的条件]
- Reduce侧联接
- 折叠Collapse插件
- linux 根据sp和fp查调用栈
- html 利用 frameset 进行简单的框架布局
- PHP的curl带验证码模拟登陆学校的教务系统(验证码已被自动识别,不需要输入)
- 02类型转化 进程空间 数组2015/08/26
- 设计模式之策略模式
- 最新制作 Mac 电脑 U 盘启动盘笔记
- javascirpt对象创建
- HDU3342 - Legal or Not 拓补排序模板
- (转)Qt中如何使用样式表QPalette以及相关注意事项
- js获取?后面具体参数的值
- Object -C NSSet -- 笔记