• HDU


    先上题目:

    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是因为这里求的是最小值。
     
    上代码:
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #define min(x,y) (x < y ? x : y)
     4 #define unequal(x,y) (x == y ? 0 : 1)
     5 #define MAX 12
     6 #define INF (1<<30)
     7 using namespace std;
     8 
     9 char s[1502][MAX];
    10 char c[MAX];
    11 int dp[MAX][MAX];
    12 
    13 int deal(int a){
    14     int l1,l2;
    15     l1=strlen(s[a]+1);
    16     l2=strlen(c+1);
    17     for(int i=0;i<=l1;i++) dp[i][0]=i;
    18     for(int j=0;j<=l2;j++) dp[0][j]=j;
    19     for(int i=1;i<=l1;i++){
    20         for(int j=1;j<=l2;j++){
    21             dp[i][j]=INF;
    22         }
    23     }
    24     for(int i=1;i<=l1;i++){
    25         for(int j=1;j<=l2;j++){
    26             dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1);
    27             dp[i][j]=min(dp[i][j],dp[i-1][j-1]+unequal(s[a][i],c[j]));
    28         }
    29     }
    30     return dp[l1][l2];
    31 }
    32 
    33 int main()
    34 {
    35     int t,n,m,e,th,count;
    36     //freopen("data.txt","r",stdin);
    37     scanf("%d",&t);
    38     for(int z=1;z<=t;z++){
    39         printf("Case #%d:
    ",z);
    40         scanf("%d %d",&n,&m);
    41         for(int i=0;i<n;i++){
    42             scanf("%s",s[i]+1);
    43         }
    44         while(m--){
    45             scanf("%s %d",c+1,&th);
    46             count=0;
    47             for(int i=0;i<n;i++){
    48                 e=deal(i);
    49                 //printf("%d ",e);
    50                 if(e<=th) count++;
    51             }
    52             //printf("
    ");
    53             printf("%d
    ",count);
    54         }
    55     }
    56     return 0;
    57 }
    4323
     
  • 相关阅读:
    [BJOI2006]狼抓兔子
    [HNOI2016]最小公倍数
    hihocoder 1419 重复旋律4
    [NOI2015]品酒大会
    [SDOI2016]生成魔咒
    [ZJOI2009]狼和羊的故事
    BZOJ4361 isn
    [SDOI2009]虔诚的墓主人
    BZOJ 3329 Xorequ
    [ZJOI2013]丽洁体
  • 原文地址:https://www.cnblogs.com/sineatos/p/3608959.html
Copyright © 2020-2023  润新知