• HDU ACM 2895-Edit distance


    Edit distance

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 39   Accepted Submission(s) : 17

    Problem Description

    Given a string, an edit script is a set of instructions to turn it into another string. There are four kinds of instructions in an edit script: Add (‘a’): Output one character. This instruction does not consume any characters from the source string. Delete (‘d’): Delete one character. That is, consume one character from the source string and output nothing. Modify (‘m’): Modify one character. That is, consume one character from the source string and output a character. Copy (‘c’): Copy one character. That is, consume one character from the source string and output the same character. Now, We define that A shortest edit script is an edit script that minimizes the total number of adds and deletes. Given two strings, generate a shortest edit script that changes the first into the second.

    Input

    The input consists of two strings on separate lines. The strings contain only alphanumeric characters. Each string has length between 1 and 10000, inclusive.

    Output

    The output is a shortest edit script. Each line is one instruction, given by the one-letter code of the instruction (a, d, m, or c), followed by a space, followed by the character written (or deleted if the instruction is a deletion).
    In case of a tie, you must generate  shortest edit script, and must sort in order of a , d, m, c. Therefore, there is only one answer.

    Sample Input

    abcde
    xabzdey
    

    Sample Output

    a x
    a a
    m b
    m z
    m d
    m e
    m y

    解题思路:

      严格按照 a(增加),d(删除),m(改变),c(复制) 的改变顺序来输出,将第一个字符串转换成第二个字符串。如  abcde  --> xabzdey, 增加(a)了x,a; 再逐个将abcde改变(m)成为bzdey。值得注意的是:如果两个字符对应相同,也不会用到copy,而要用m,如  abc --> abc ,用 m 的结果是 m a, m b, m c; 用 c 的结果是c a, c b, c d;但是遵循m在c之前(最简)原则,必须用m,其实就是没有要用c的时候,可以用c的地方,就一定可以用m来代替。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #define MAXN 10000
     4 char s1[MAXN],s2[MAXN];
     5 int main()
     6 {
     7     int n1,n2,n,i;
     8     while(scanf("%s",s1)!=EOF)
     9     {
    10         scanf("%s",s2); 
    11         n1=strlen(s1);
    12         n2=strlen(s2); 
    13         n=n2-n1;
    14         if(n>0)
    15         {
    16             for(i=0;i<n2;i++)
    17             {
    18                 if(n>0)
    19                 {
    20                     printf("a %c
    ",s2[i]);
    21                     n--;
    22                 }
    23                 else    printf("m %c
    ",s2[i]);
    24             } 
    25         }
    26         else if(n==0)
    27         {
    28             for(i=0;i<n2;i++)    
    29             printf("m %c
    ",s2[i]);
    30         }
    31         else{
    32             for(i=0;i<n1;i++)
    33             {
    34                 if(n<0)
    35                 {
    36                     printf("d %c
    ",s1[i]);
    37                     n++;
    38                 }
    39             }
    40             for(i=0;i<n2;i++)
    41                 printf("m %c
    ",s2[i]);    
    42         }
    43     }    
    44     return 0;
    45 }
  • 相关阅读:
    RHEL6.5上安装Oracle11gR2
    20171123_Python学习六周四次课(11月23日)
    查看CentOS版本信息:
    20171122_Python学习六周三次课(11月22日)
    20171121_Python学习六周二次课(11月21日)
    20171120_Python学习六周一次课(11月20日)
    20171115_Python学习五周三次课
    20171114_Python学习五周二次课
    20171113_Python学习五周一次课
    20171110_Python学习四周五次课
  • 原文地址:https://www.cnblogs.com/yeshadow937/p/3893712.html
Copyright © 2020-2023  润新知