先上题目:

Magic Number

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

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
 
  题意,给你一系列的数字(n个),然后又m个询问,每个询问给你一个数字和一个最大变换次数,问你把这些数字当成字符串,在限定的转换次数里面将询问的数字变成n个数字里面的某一个数字,转换方式如最先编辑距离,问符合条件的数字有多少个。
  这题其实就是最小编辑距离的扩展版,需要注意的是,在读入数字的时候千万不要用数字读入再转为字符串,否者有可能会WA。
  这里总结一下最小编辑距离。
  
  char A[MAX],B[MAX]:字符串数组
  dp[i][j] :表示要令A[0,i]变成与B[0,j]一样,至少需要变换多少次。
  
  变换的方法有插入,删除,将某一个字符变成另一个特定的字符。
  ①插入: 如果在A[i]后面一个字符X,使得A[0,i]==B[0,j](即A[0,i-1]==B[0,j-1],在A[i]位置再加一个X(B[j]) )    那么dp[i][j]=dp[i][j-1]+1;    对于B同理。
  ②删除: 如果删除A[i]位置的字符X,使得A[0,i]==B[0,j](即A[0,i-1]==B[0,j],将A[i]删除)               那么dp[i][j]=dp[i-1][j]+1;  对于B同理。
  ③转换: 如果将A[i]变成B[j],那么如果原本A[i]==B[j],那么转换次数就是0,否者就是1。
  状态转移方程就是dp[i][j]=min{dp[i][j-1]+1,dp[i-1][j]+1,dp[i-1][j-1]+(A[i]!=B[j] ? 1 : 0)}
  
  需要注意的地方还有一个,在初始化的时候:dp[i][0]=i,dp[0][j]=j,dp[i][j]=INF。意思是将一个空字符串转化为一个长度为i(j)的字符串需要i(j)步,这是边界条件。而其他都是初始化为INF是因为这里求的是最小值。
 
上代码:
 
 #include <cstdio>
#include <cstring>
#define min(x,y) (x < y ? x : y)
#define unequal(x,y) (x == y ? 0 : 1)
#define MAX 12
#define INF (1<<30)
using namespace std; char s[][MAX];
char c[MAX];
int dp[MAX][MAX]; int deal(int a){
int l1,l2;
l1=strlen(s[a]+);
l2=strlen(c+);
for(int i=;i<=l1;i++) dp[i][]=i;
for(int j=;j<=l2;j++) dp[][j]=j;
for(int i=;i<=l1;i++){
for(int j=;j<=l2;j++){
dp[i][j]=INF;
}
}
for(int i=;i<=l1;i++){
for(int j=;j<=l2;j++){
dp[i][j]=min(dp[i-][j]+,dp[i][j-]+);
dp[i][j]=min(dp[i][j],dp[i-][j-]+unequal(s[a][i],c[j]));
}
}
return dp[l1][l2];
} int main()
{
int t,n,m,e,th,count;
//freopen("data.txt","r",stdin);
scanf("%d",&t);
for(int z=;z<=t;z++){
printf("Case #%d:\n",z);
scanf("%d %d",&n,&m);
for(int i=;i<n;i++){
scanf("%s",s[i]+);
}
while(m--){
scanf("%s %d",c+,&th);
count=;
for(int i=;i<n;i++){
e=deal(i);
//printf("%d ",e);
if(e<=th) count++;
}
//printf("\n");
printf("%d\n",count);
}
}
return ;
}

4323

 

HDU - 4323 - Magic Number的更多相关文章

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

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

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

  3. Magic Number(Levenshtein distance算法)

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

  4. 一个快速double转int的方法(利用magic number)

    代码: int i = *reinterpret_cast<int*>(&(d += 6755399441055744.0)); 知识点: 1.reinterpret_cast&l ...

  5. hdu 5898 odd-even number 数位DP

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

  6. LVM XFS增加硬盘分区容量(resize2fs: Bad magic number in super-block while)

    LVM XFS增加硬盘分区容量(resize2fs: Bad magic number -- :: 分类: Linux LVM XFS增加硬盘分区容量(resize2fs: Bad magic num ...

  7. hdu 2665 Kth number

    划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...

  8. [ZOJ 3622] Magic Number

    Magic Number Time Limit: 2 Seconds      Memory Limit: 32768 KB A positive number y is called magic n ...

  9. poj magic number

    Problem H Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

随机推荐

  1. 查看tensorflow pb模型文件的节点信息

    查看tensorflow pb模型文件的节点信息: import tensorflow as tf with tf.Session() as sess: with open('./quantized_ ...

  2. bzoj 3231 [ Sdoi 2008 ] 递归数列 —— 矩阵乘法

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3231 裸矩阵乘法. 代码如下: #include<iostream> #incl ...

  3. 实现泛型IEnumerable接口

    用C#实现一个类的IEnumerable接口时有两种方法:1)实现非泛型IEnumerable接口:2)实现泛型IEnumerable(T)接口.如果采用方法1,当集合元素T是值类型时,将涉及到巨多的 ...

  4. Spark底层原理简化版

    目录 Spark SQL/DF的执行过程 集群运行部分 Aggregation Join Shuffle Tungsten 内存管理机制 缓存敏感计算(Cacheaware computation) ...

  5. (Go)05.基本数据类型和操作符

    保留的关键字段 1.Question1 package main import ( "fmt" ) func list(n int) { ; i <= n; i++ { fm ...

  6. E20170808-mk

    Backtick  反引号 import n. 输入; 进口,进口商品 triggered  adj. 触发的;

  7. C Looooops(扩展欧几里得+模线性方程)

    http://poj.org/problem?id=2115 题意:给出A,B,C和k(k表示变量是在k位机下的无符号整数),判断循环次数,不能终止输出"FOREVER". 即转化 ...

  8. 2015 多校赛 第四场 1009 (hdu 5335)

    Problem Description In an n∗m maze, the right-bottom corner is the exit (position (n,m) is the exit) ...

  9. css的选择器效率分析

    我们都知道,CSS具有叠加性(同一个元素被多条样式规则指定),继承性(后代元素会继承前辈元素的一些样式和属性)和优先级 (由于CSS的叠加性和继承性,将产生优先级,这指的是哪条样式规则会最终作用于指定 ...

  10. Unity引擎GUI之Input Field

    InputField 文本输入组件,本文练习InputField的属性及事件 一.属性 1 Interactable: 是否禁用 Transition:过渡方式 Normal Color 正常的未有任 ...