hdu 4323 Magic Number( DP )

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4323

Magic Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1728    Accepted Submission(s): 705

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

Author

BJTU

分析:比较简单的dp,分析好dp转移方程即可,不要分析错了。。。。

思路: 用dp[ i ] [ j ]表示第一个串的前i个数字,第二个串的前j个数字匹配所需要的最少操作次数;

转移方程:

dp[i][j]=dp[i-1][j-1]  if(a[i]==b[j])

dp[i][j]=min( dp[i-1][j]+1 ,  min(dp[i-1][j-1]+1,dp[i][j-1]+1  )  ) //分别对应着 删除,替换,插入操作;

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
char magic[2200][22];
int dp[22][22];

int main()
{
  int T,test=1;
  scanf("%d",&T);

  while( T-- )
  {
    int n,m,d;
    scanf("%d%d",&n,&m);
    memset(magic,'\0',sizeof(magic));
    for(int i=1;i<=n;i++)scanf("%s",magic[i]+1);
    printf("Case #%d:\n",test++);

    while(m--)
    {
      char op[22];
      memset(op,'\0',sizeof(op));
      scanf("%s%d",op+1,&d);
      int len2=strlen(op+1);
      int ans=0;
      for(int i=1;i<=n;i++)
      {
          int len1=strlen(magic[i]+1);
          memset(dp,0,sizeof(dp));

          for(int j=1;j<=len2;j++)dp[0][j]=j;
          for(int j=1;j<=len1;j++)dp[j][0]=j;

          for(int j=1;j<=len1;j++)
             for(int k=1;k<=len2;k++)
             {
                if(magic[i][j]==op[k])dp[j][k]=dp[j-1][k-1];
                else
                {
                   dp[j][k]=min(dp[j-1][k-1]+1,min(dp[j-1][k]+1,dp[j][k-1]+1));
                }
             }
          if(dp[len1][len2]<=d)ans++;
      }
        printf("%d\n",ans);
    }
  }
  return 0;
}
时间: 2024-08-27 04:19:25

hdu 4323 Magic Number( DP )的相关文章

hdu 4323 Magic Number (dp,编辑距离)

链接:hdu 4323 题意:给定n个串和m次询问,对于每次询问,给定一个字符串t,和最大操作次数a, 问在n个字符串中有多少个能在规定的次数之内变成字符串t. 说明:字符串的基本操作仅为:删除.插入和修改一个字符这三种操作 我们把进行了一次上述三种操作的任意一种操作称为进行了一步字符基本操作. 两个字符串的编辑距离:两个字符串a和b,通过上述的基本操作,把a变成b或b变成a, 需要的最少基本字符操作步数称为字符串a和字符串b的编辑距离 分析:分别求出n个字符串与字符串t之间的编辑距离,并判断是

HDU 4323 Magic Number(编辑距离DP)

http://acm.hdu.edu.cn/showproblem.php?pid=4323 题意: 给出n个串和m次询问,每个询问给出一个串和改变次数上限,在不超过这个上限的情况下,n个串中有多少个串可以转化为询问中给的串. 思路: 明显的编辑距离DP,直接暴力过了,网上有用bk树的,可惜我不会. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6

hdu 5125 magic balls(dp)

题目链接:hdu 5125 magic balls #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1005; int N, M, dp[maxn][maxn], A[maxn], B[maxn]; int vec[maxn][maxn], c[maxn]; void init () { scanf("%d%d&quo

HDU 5125 Magic Ball DP+树状数组

由于只要找1~x 中的最大值,然后线段树又容易MLE,所以这里可以用树状数组搞. #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <set> #include <bitset> #include <queue> #include <stack> #include <string> #i

hdu 3006 The Number of set(思维+壮压DP)

The Number of set Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1056    Accepted Submission(s): 655 Problem Description Given you n sets.All positive integers in sets are not less than 1 and

hdu 5787 K-wolf Number 数位dp

数位DP 神模板 详解 为了方便自己参看,我把代码复制过来吧 // pos = 当前处理的位置(一般从高位到低位) // pre = 上一个位的数字(更高的那一位) // status = 要达到的状态,如果为1则可以认为找到了答案,到时候用来返回, // 给计数器+1. // limit = 是否受限,也即当前处理这位能否随便取值.如567,当前处理6这位, // 如果前面取的是4,则当前这位可以取0-9.如果前面取的5,那么当前 // 这位就不能随便取,不然会超出这个数的范围,所以如果前面取

hdu 3709 Balanced Number (数位dp)

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1871    Accepted Submission(s): 836 Problem Description A balanced number is a non-negative integer that can be balanced if a pi

HDU 3709 Balanced Number 枚举+数位DP

枚举支点之后数位DP,注意姿势 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list&g

Magic Number(Levenshtein distance算法)

Magic Number Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4323 Description There are many magic numbers whose lengths are less than 10. Given some queries, each contains a single number, if t