码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4323 Magic Number( DP )

时间:2015-01-26 13:45:10      阅读:116      评论:0      收藏:0      [点我收藏+]

标签: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;
}


hdu 4323 Magic Number( DP )

标签:dp

原文地址:http://blog.csdn.net/liusuangeng/article/details/43150553

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!