描述:

  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. 菜鸟学SSH(十八)——Hibernate动态模型+JRebel实现动态创建表

    项目用的是SSH基础框架,当中有一些信息非常相似,但又不尽同样.假设每个建一个实体的话,那样实体会太多.假设分组抽象,然后继承,又不是特别有规律.鉴于这样的情况.就打算让用户自己配置要加入的字段,然后 ...

  2. tabBar隐藏与显现 hidesBottomBarWhenPushed

    这个问题说简单也简单  但是如果不知道 可会让很多人吃苦 隐藏UITabBarController的tabBar, 我用它的一个属性hidesBottomBarWhenPushed隐 藏了,可以pop ...

  3. 【POJ 1125】Stockbroker Grapevine

    id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...

  4. 2015-01-15百度地图API 新海量点

    整理一下昨天写的百度地图 项目最开始写了一个百度地图,但是速度那慢的简直了 所以昨天将百度地图API的海量点 写了一下 1秒啊 o.o  厉害 OK 记下 此乃需要的js <!--添加百度地图- ...

  5. create a (VSTO) Office 2007 add-in using VS 2012?

    You can get VS 2012 working with Office 2007. First create an Outlook 2010 Add-In and modify the pro ...

  6. Adroid: getExternalStorageDirectory 不一定是你想要的外部存储SdCard

    前情提要:我的测试机是华为荣耀6,我装过一个16G的内存卡 因为要面试的需要,我的一个演示项目用的是android本地的WebService.然而写好的webService部署到本地上,应用怎么获取数 ...

  7. Java SE基础部分——常用类库之SimpleDateFormat(日期格式化)

    取得当前日期,并按照不同日期格式化输入.代码如下: // 20160618 SimpleDateFomat类的使用 日期格式化 练习 package MyPackage; //自己定义的包 impor ...

  8. nginx upstream setting

    upstream proxy_1 { server 127.0.0.1:8080; #连接到上游服务器的最大并发空闲keepalive长连接数(默认是未设置,建议与Tomcat Connector中的 ...

  9. 百度地图Label 样式:label.setStyle

    创建文本标注对象设置样式的时候,其中的backgroundColor属性居然还支持透明啊,不过改变数值好像对效果没有影响 var numLabel = new BMap.Label(num); num ...

  10. ORACLE同义词总结

    ORACLE同义词总结 同义词概念 Oracle的同义词(synonyms) 从字面上理解就是别名的意思,和视图的功能类似,就是一种映射关系.它可以节省大量的数据库空间,对不同用户的操作同一张表没有多 ...