描述:

  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).

  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.

  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.

代码:

  这里提到了levenshtein distance,特去维基百科查阅。In information theory and computer science, the Levenshtein distance is a string metric for measuring the difference between two sequences.Levenshtein distance between two words is the minimum number of single-character edits (i.e. insertions, deletions or substitutions) required to change one word into the other.也就是从一个字符串经过增、删、改变换到另一个字符串所需要的最少操作步骤。

  求levenshtein distance有已有的算法:

    Mathematically, the Levenshtein distance between two strings  (of length  and  respectively) is given by  where

  

    where  is the indicator function equal to 0 when  and equal to 1 otherwise.

  先描述一下算法的原理:

  • 如果我们可以使用k个操作数把s[1…i]转换为t[1…j-1],我们只需要把t[j]加在最后面就能将s[1…i]转换为t[1…j],操作数为k+1。
  • 如果我们可以使用k个操作数把s[1…i-1]转换为t[1…j],我们只需要把s[i]从最后删除就可以完成转换,操作数为k+1。
  • 如果我们可以使用k个操作数把s[1…i-1]转换为t[1…j-1],我们只需要在需要的情况下(s[i] != t[j])把s[i]替换为t[j],所需的操作数为k+cost(cost代表是否需要转换,如果s[i]==t[j],则cost为0,否则为1)。

  为了更加清晰的理解,我们用一个二维表来理解:

    b e a u t y
  0 1 2 3 4 5 6
b 1            
a 2            
t 3            
y 4            
u 5            

  初始的时候,第一行与第一列初始化为0-n,代表利用insertion操作从空串依次插入,得到当前的串。

    b e a u t y
  0 1 2 3 4 5 6
b 1          
a 2            
t 3            
y 4            
u 5            

  dp[1][1]的值由左方、上方和左前方的值决定。从左方来,意味着从beauty的空到batyu的b,只需要一次insertion操作,操作数和为1+1;从上方来与从左方来类似,操作数为1+1;从左上方来,由于beauty的b与batyu的b相等,所以不需要进行操作,操作数为0+0。选取最小值0+0位dp值。

    b e a u t y
  0 1 2 3 4 5 6
b 1        
a 2          
t 3            
y 4            
u 5          

  同理,我们可以填出其他值。

  根据算法的原理,可以解决这道题。这道题求的是一大堆串与给定串的编辑距离小于等于给定threshold(阈值)的个数。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define N 15
#define M 1505 int MIN( int a,int b,int c ){
if( b<a ) a=b;
if( c<a ) a=c;
return a;
} int main(){
int T,tc=,count,magic_num,query_num,threshold,dp[N][N],cost;
char magic[M][N],query[N];
scanf("%d",&T);
while( tc<=T ){
scanf("%d%d",&magic_num,&query_num);
for( int i=;i<magic_num;i++ )
scanf("%s",magic[i]);
for( int i=;i<N;i++ ){
dp[][i]=i;
dp[i][]=i;
}
printf("Case #%d:\n",tc);
while( query_num-- ){
scanf("%s%d",&query,&threshold); count=;
for( int i=;i<magic_num;i++ ){
for( int j=;j<=strlen(magic[i]);j++ ){
for( int k=;k<=strlen(query);k++ ){
if( magic[i][j-]==query[k-] )
cost=;
else
cost=;
dp[j][k]=MIN(dp[j-][k]+,dp[j][k-]+,dp[j-][k-]+cost);
}
}
if( dp[strlen(magic[i])][strlen(query)]<=threshold )
count++;
}
printf("%d\n",count);
}
tc++;
}
system("pause");
return ;
}

HDU4323-Magic Number(levenshtein distance-编辑距离)的更多相关文章

  1. Levenshtein distance 编辑距离算法

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

  2. 利用Levenshtein Distance (编辑距离)实现文档相似度计算

    1.首先将word文档解压缩为zip /** * 修改后缀名 */ public static String reName(String path){ File file=new File(path) ...

  3. Levenshtein distance 编辑距离

    编辑距离,又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符 实现方案: 1. 找出最长 ...

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

    编辑距离即从一个字符串变换到另一个字符串所需要的最少变化操作步骤(以字符为单位,如son到sun,s不用变,将o->s,n不用变,故操作步骤为1). 为了得到编辑距离,我们画一张二维表来理解,以 ...

  5. Magic Number(Levenshtein distance算法)

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

  6. HDU 4323——Magic Number——————【dp求编辑距离】2012——MUT——3

    Magic Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

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

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

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

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

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

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

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

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

随机推荐

  1. jcenter那些事儿

    jcenter是一个server托管在bintray.com的maven仓库. in project's build.gradle file allprojects { repositories { ...

  2. LCD与ARM,具体

    一  实验内容简要描写叙述 1.实验目的 学会驱动程序的编写方法,配置S3C2410的LCD驱动,以及在LCD屏上显示包含bmp和jpeg两种格式的图片 2.实验内容  (1)分析S3c2410实验箱 ...

  3. [STL源码剖析]RB-tree的插入操作

    RB-tree的性质 对于RB-tree,首先做一个了解,先看一张维基百科的RB-tree: 再看RB-tree的性质: 性质1. 节点是红色或黑色. 性质2. 根是黑色,所有叶子都是黑色(叶子节点指 ...

  4. curl多线程类。

    <?php /* * Curl 多线程类 * 使用方法: * ======================== $urls = array("http://baidu.com" ...

  5. Linux学习之给指定用户发邮件

    发送邮件 进入 mail 程序后的操作都很简单,但是可以不进入 mail 的 & 操作提示符界面,下面举几个实用例子: 1.给 snailwarrior@qq.com 发信 [root@pps ...

  6. mysql服务端安装的系列问题处理

    1.mysql服务无法启动,报1067错误 注意:mysql服务端加压之后的文件名最好不要带其他符号(如:"_"之类等其他的一些符号),用纯英文跟数字就可以,没有经过仔细验证带符号 ...

  7. c语言题目:找出一个二维数组的“鞍点”,即该位置上的元素在该行上最大,在该列上最小。也可能没有鞍点

    //题目:找出一个二维数组的“鞍点”,即该位置上的元素在该行上最大,在该列上最小.也可能没有鞍点. // #include "stdio.h" #include <stdli ...

  8. cocos2d-x -------之笔记篇 环境的安装

    cocos2d-x -------之笔记篇 环境的安装 使用到的工具有VS2010  cygwin android-NDK eclipse android SDK 1.首先是android相关环境的安 ...

  9. 17.java.lang.CloneNotSupportedException

    java.lang.CloneNotSupportedException不支持克隆异常 当没有实现Cloneable接口或者不支持克隆方法时,调用其clone()方法则抛出该异常.

  10. Delphi通过IE窗口句柄获取网页接口(IWebBrowser2) good

    主要用到的是MSAA(Microsoft Active Accessibility) 函数:ObjectFromLResult,该函数在动态链接库 oleacc.dll 中定义. uses SHDoc ...