描述:

  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. ASP.NET动态引用WebService接口

    尊重原著作:本文转载自http://www.mhzg.net/a/20124/20124912180589.html 有经验的朋友都知道,通常我们在引用webservice的时候,是在项目中就添加了引 ...

  2. trim()函数IE7/8不兼容

    js中重写trim()函数 <script type="text/javascript">     String.prototype.trim = function() ...

  3. 第9课_3_db库安装

    四 安装listener监听 使用oracle用户,在rac1和rac2上都可以起netca 1.打开网络配置界面,选择"Cluster configuration"集群配置方案, ...

  4. VB.NET函数——数学函数/字母串函数

    一.数学函数 函数 说明 Abs (num) 取绝对值. Exp (num) 返回以e为底.以num为指数的值,如Exp(2)返回e^2值. Log (num) 返回参数num的自然对数值,为Doub ...

  5. HighlightingSystem插件使用(边缘发光)

    插件链接: http://pan.baidu.com/s/1dFwkaTr 密码: nw2c 导入Unity里面可能会报错,不过没关系,直接注释掉就可以了,我用的是Unity5.1的版本 可以看到如下 ...

  6. 浅谈JavaScript中的内存管理

    一门语言的内存存储方式是我们学习他必须要了解的,接下来让我浅谈一下自己对他的认识. 首先说,JavaScript中的变量包含两种两种类型: 1)值类型或基本类型:undefined.null.numb ...

  7. 开发环境配置--Ubuntu+Qt4+OpenCV(二)

    同系列文章 1. 开发环境配置--Ubuntu+Qt4+OpenCV(一) 2. 开发环境配置--Ubuntu+Qt4+OpenCV(二) 3. 开发环境配置--Ubuntu+Qt4+OpenCV(三 ...

  8. Linux 如何打开端口

    举例: 开放10000端口的解决步骤如下: 1.修改/etc/sysconfig/iptables文件,增加如下一行: -A RH-Firewall-1-INPUT -m state --state ...

  9. Oracle EBS-SQL (SYS-3):sys_人员用户名对应关系查询.sql

    select fu.user_name 用户名,       fu.description 描述,       (select ppf.FULL_NAME          from per_peop ...

  10. [Leetcode][Python][DP]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...