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\n",s2[i]);
21                     n--;
22                 }
23                 else    printf("m %c\n",s2[i]);
24             } 
25         }
26         else if(n==0)
27         {
28             for(i=0;i<n2;i++)    
29             printf("m %c\n",s2[i]);
30         }
31         else{
32             for(i=0;i<n1;i++)
33             {
34                 if(n<0)
35                 {
36                     printf("d %c\n",s1[i]);
37                     n++;
38                 }
39             }
40             for(i=0;i<n2;i++)
41                 printf("m %c\n",s2[i]);    
42         }
43     }    
44     return 0;
45 }

 

 

转载于:https://www.cnblogs.com/yeshadow937/p/3893712.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值