Magic Number

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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为字符串长度。

Magic Number(Levenshtein distance算法)的更多相关文章

  1. Levenshtein Distance算法(编辑距离算法)

    编辑距离 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符, ...

  2. 字符串相似度算法——Levenshtein Distance算法

    Levenshtein Distance 算法,又叫 Edit Distance 算法,是指两个字符串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一 ...

  3. 字符串相似度算法-LEVENSHTEIN DISTANCE算法

    Levenshtein Distance 算法,又叫 Edit Distance 算法,是指两个字符串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一 ...

  4. 编辑距离算法详解:Levenshtein Distance算法

    算法基本原理:假设我们可以使用d[ i , j ]个步骤(可以使用一个二维数组保存这个值),表示将串s[ 1…i ] 转换为 串t [ 1…j ]所需要的最少步骤个数,那么,在最基本的情况下,即在i等 ...

  5. Levenshtein distance 编辑距离算法

    这几天再看 virtrual-dom,关于两个列表的对比,讲到了 Levenshtein distance 距离,周末抽空做一下总结. Levenshtein Distance 介绍 在信息理论和计算 ...

  6. 扒一扒编辑距离(Levenshtein Distance)算法

    最近由于工作需要,接触了编辑距离(Levenshtein Distance)算法.赶脚很有意思.最初百度了一些文章,但讲的都不是很好,读起来感觉似懂非懂.最后还是用google找到了一些资料才慢慢理解 ...

  7. Levenshtein Distance(编辑距离)算法与使用场景

    前提 已经很久没深入研究过算法相关的东西,毕竟日常少用,就算死记硬背也是没有实施场景导致容易淡忘.最近在做一个脱敏数据和明文数据匹配的需求的时候,用到了一个算法叫Levenshtein Distanc ...

  8. C#实现Levenshtein distance最小编辑距离算法

    Levenshtein distance,中文名为最小编辑距离,其目的是找出两个字符串之间需要改动多少个字符后变成一致.该算法使用了动态规划的算法策略,该问题具备最优子结构,最小编辑距离包含子最小编辑 ...

  9. 字符串相似度算法(编辑距离算法 Levenshtein Distance)(转)

    在搞验证码识别的时候需要比较字符代码的相似度用到“编辑距离算法”,关于原理和C#实现做个记录. 据百度百科介绍: 编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指两个 ...

随机推荐

  1. Ubuntu修改mysql默认编码的方法

    ubuntu使用apt-get安装mysql后,server的默认编码是latin1,下面把server的编码修改成utf8. 编码相关信息: 1 2 3 4 5 6 7 8 9 10 11 12 1 ...

  2. Javascript面向对象编程

    https://segmentfault.com/a/1190000002900676 介绍 和java这种基于类(class-base)的面向对象的编程语言不同,javascript没有类这样的概念 ...

  3. JavaWeb---总结(十一)使用Cookie进行会话管理

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...

  4. tomcat并发

    Tomcat的最大并发数是可以配置的,实际运用中,最大并发数与硬件性能和CPU数量都有很大关系的.更好的硬件,更多的处理器都会使Tomcat支持更多的并发. Tomcat默认的HTTP实现是采用阻塞式 ...

  5. xfce4 dev tools的一些说明

    xfce4 dev tools实际上基本是封装了一些autoconf的宏函数 比如XDT_I18N: AC_DEFUN([XDT_I18N], [ dnl Substitute GETTEXT_PAC ...

  6. css006 文本格式化

    css006 文本格式化 文本格式化:字体(font-family).颜色(color).字号(font-size). 行距(line-height).粗体(font-weight).斜体(font- ...

  7. Linux开放1521端口允许网络连接Oracle Listene

    症状:1. TCP/IP连接是通的.可以用ping 命令测试. 2. 服务器上Oracle Listener已经启动.  lsnrctl status  查看listener状态  lsnrctl s ...

  8. centos 6.4 系统代理上网 设置

    前面讲了yum 代理设置上网的方法,现在设置一下系统代理上网, 网上很多都不管用,已亲测管用 [root@proxy ~]# cat .bash_profile #root目下添加代理上网,蓝色代码# ...

  9. AspectJ基础学习之一简介(转载)

    AspectJ基础学习之一简介(转载) 一.为什么写这个系列的博客   Aspectj一个易用的.功能强大的aop编程语言.其官网地址是:http://www.eclipse.org/aspectj/ ...

  10. debian vmwareTools安装总结

    1.安装GCC编译器和make:      因为安装VMwareTools需要编译和make所以要先安装它们.安装其实很简单,命令如下: apt-get install gcc make 其实你也可以 ...