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. scala break & continue

    Scala没有提供break和continue,我们可以自己实现一个,参考例子: import util.control.Breaks._ object BreakDemo { def main(ar ...

  2. python获取知乎日报另存为txt文件

    前言 拿来练手的,比较简单(且有bug),欢迎交流~ 功能介绍 抓取当日的知乎日报的内容,并将每篇博文另存为一个txt文件,集中放在一个文件夹下,文件夹名字为当日时间. 使用的库 re,Beautif ...

  3. JSTL的if-else表式

    JSTL用法,这里不细讲了,主要是if-else的写法: 代码片段: <c:choose> <c:when test="${user.role eq 1 }"&g ...

  4. IOS - 打印COOKIE中的 CRFSToken

    NSHTTPCookie 在iOS中使用NSHTTPCookie类封装一条cookie,通过NSHTTPCookie的方法读取到cookie的通用属性. - (NSUInteger)version; ...

  5. 点亮第一个LED灯

    1.代码: #include <reg52.h> //<reg51.h>  包含52单片机寄存器库sbit led = P1^0;    //只有地址可以被8整除的 才可以用s ...

  6. glade2支持C++代码的输出(1)

    开发了一个基类,用于支持GTK2的信号回调 见BaseObject.zip 为了便于快速通过glade设计界面,并生成相应的C++代码,我对glade-2 2.12.2的代码进行了修改 原始代码:gl ...

  7. C#中值类型和引用类型图解

    举几个值类型和引用类型的内存配置: 值类型存储在栈中,引用类型堆里: 1,数组 数组是引用类型,但是数组的元素可以是值类型或引用类型 2. 结构 结构是值类型,简略的看个例子 struct sampl ...

  8. 20145212 《Java程序设计》第1周学习总结

    20145212 <Java程序设计>第1周学习总结 教材学习内容总结 看了毕向东老师的视频,我对Java有了进一步的了解.相比于其他的计算机编程语言(比如C语言),Java有一大特点就是 ...

  9. MySQL学习笔记——多表连接和子查询

    多表连接查询 # 返回的是两张表的乘积 SELECT * FROM tb_emp,tb_dept SELECT COUNT(*) FROM tb_emp,tb_dept # 标准写法,每个数据库都能这 ...

  10. 内嵌DB

    SQLLite H2 MySQL Embeded 等 比较项目 SQLite H2 database engine MySQL Embedded Footprint 350KiB ~1MB <2 ...