Magic Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1867    Accepted Submission(s): 763

Problem 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
 
 
题目大意:给你t组数据。每组有n个数字串,有m个询问,每个询问有一个数字串和一个次数阀值,问你将n个数字串转化成询问数字串的编辑次数不大于阀值的有多少个。编辑距离是指:将一个串通过增加删除替换变成另外的串的最少次数。
 
解题思路:想过kmp,感觉不太靠谱,想到判断最长公共子序列,写到一半没再写了。后来看别人说可以直接dp,然后发现其实这个dp跟最长公共子序列的dp比较像。这里定义dp[i][j]为s串的前i个字符跟t串的前j个字符形成的编辑距离。
如果s[i]!=s[j] dp[i][j]=min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])。
如果s[i]==s[j] dp[i][j]=dp[i-1][j-1]。
同时注意初始化应该让dp[i][0]=i。dp[0][j]=i。表示跟空串进行转化的编辑距离。
 
#include<bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
char Map[1600][15], str[15];
int len[1600];
int dp[20][20];
int main(){
int t,cnt=0,n,m;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%s",Map[i]);
len[i]=strlen(Map[i]);
}
printf("Case #%d:\n",++cnt);
int lim;
while(m--){
scanf("%s%d",str,&lim);
int lens=strlen(str);
int sum=0;
for(int k=1;k<=n;k++){
if(abs(len[k]-lens)<=lim){
memset(dp,0,sizeof(dp));
for(int i=1;i<=lens;i++)
dp[i][0]=i;
for(int j=1;j<=len[k];j++)
dp[0][j]=j;
for(int i=1;i<=lens;i++){
for(int j=1;j<=len[k];j++){
if(str[i-1]==Map[k][j-1]){
dp[i][j]=dp[i-1][j-1];
}else{
dp[i][j]=min(min(dp[i-1][j-1],dp[i][j-1]),dp[i-1][j])+1;
} }
}
if(dp[lens][len[k]]<=lim)
sum++;
}
}
printf("%d\n",sum);
}
}
return 0;
}

  

HDU 4323——Magic Number——————【dp求编辑距离】2012——MUT——3的更多相关文章

  1. HDU - 4323 - Magic Number

    先上题目: Magic Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  2. HDU 4323 Magic Number(编辑距离DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4323 题意: 给出n个串和m次询问,每个询问给出一个串和改变次数上限,在不超过这个上限的情况下,n个串中有多少个 ...

  3. HDU 3853 LOOP (概率DP求期望)

    D - LOOPS Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit St ...

  4. HDU 5623KK's Number DP

    题意:bc round 71 div 1 1003(有中文题面) 分析: 显然,每个人的策略就是都会拿剩下的数中最大的某几个数 假如我们用dp[i]表示当剩下i个数的时候先手得分-后手得分的最优值 那 ...

  5. HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)

    Problem Description A password locker with N digits, each digit can be rotated to 0-9 circularly.You ...

  6. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

  7. HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  8. Magic Number(Levenshtein distance算法)

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

  9. hdu4323Magic Number(dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=4323 去年的多校 编辑距离的变形 暴力居然过了 还想了好久别的方法,想得很头疼 #include <ios ...

随机推荐

  1. C#使用Newtonsoft.Json操作json

    1.下载 Newtonsoft.Json 右键引用,选择 管理NuGet程序包,选择浏览,输入log4net,然后点击下载 2.json数据的组装与解析 public class Class { pu ...

  2. UIPageViewController

    前言 iPhone 和 iPad 都是通过页控件来展示多个桌面,很多 App 在第一次使用时也会使用页控件来介绍自己的功能,页控件的交互效果非常好,适用于把几个简单的页面充分展示出来. 1.UIPag ...

  3. H - the Sum of Cube(水题)

    A range is given, the begin and the end are both integers. You should sum the cube of all the intege ...

  4. 微信小程序强制横屏办法

    最近想学习学习微信小程序开发,本着先设计,再查找具体实现的方法的想法,在进行数据统计时,想着竖屏展示数据会造成重叠,或者数据显示不全而用省略号代替的问题,所以计划采用横屏的方式显示数据表格. 搜索到两 ...

  5. There is no row in position 0

    更改程序池 管道模式 ---->经典    常见设置问题: 32位启用

  6. uversion5 怎么添加设备

    实时 点击网址去它的官网下载,然后选择自己的设备组,Dfg ,下载下来的是一个安装包,直接安装即可

  7. Qt 学习之路 2(9):资源文件

    Qt 学习之路 2(9):资源文件  豆子  2012年8月31日  Qt 学习之路 2  62条评论 上一章节中我们介绍了如何使用QAction添加动作.其中,我们使用QIcon加载了一张 png ...

  8. Solr学习笔记(1) —— Solr概述&Solr的安装

    一.概述 使用Solr实现电商网站中商品信息搜索功能,可以根据关键字.分类.价格搜索商品信息,也可以根据价格进行排序. 1.1 实现方法 在一些大型门户网站.电子商务网站等都需要站内搜索功能,使用传统 ...

  9. hdu Minimum Inversion Number(逆序数的小知识与线段树)

    飞! 题解 首先,求逆序数对的思路: 1.得到整个数列后,从前往后扫,统计比a[i]小的,在a[i]后面的有多少个 这样做的话,应该是只有n2的暴力作法,没想到更好的方法 2.统计a[i]前面的,且比 ...

  10. Oracle外连接与条件的组合

    由于很少使用SQL 92语法,今天写个outer join的时候被搞晕了.参考了一些例子后整理如下.总结,"inter join on"中的条件是对table进行joining的r ...