• 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 }
  • 相关阅读:
    SSM框架整合(Spring+SrpingMVC+Mybatis) 简单案例
    SpringDataRedis操作Redis简单案例
    SpringMVC总结四:拦截器简单介绍
    Configure a bridge interface over a VLAN tagged bonded interface
    Create a bridge using a tagged vlan (8021.q) interface
    Configure a VLAN (on top of a bond) with NetworkManager (nmcli) in RHEL7
    Configure bridge on a team interface using NetworkManager in RHEL 7
    Configure a bridged network interface for KVM using RHEL 5.4 or later?
    程序员的成长阶梯和级别定义
    <程序员从入门到精通> -- How
  • 原文地址:https://www.cnblogs.com/yeshadow937/p/3893712.html
Copyright © 2020-2023  润新知