HDU 2895 Edit distance(数学+模拟)

来源:互联网 发布:赛诺数据官网 编辑:程序博客网 时间:2024/05/29 19:17

转载请注明出处:http://blog.csdn.net/u012860063

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2895


Edit distance

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 256    Accepted Submission(s): 123

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
abcdexabzdey
 
Sample Output
a xa am bm zm dm em y
 

Source
2009 Multi-University Training Contest 10 - Host by NIT

PS:

C 完全可以不用的;

题意:

给你两个字符串,要求用最少的步骤将字符串1变为字符串2
a   #   表示将字符#加入字符串2中
d   #   表示删除字串1中的一个字符

m  #   表示删除串1中一个字符并将#加入字符串2中

思路:

实际上产生距离的就只有add 和 delete,因为modify是不算距离的,那么两个字符串之间的最小编辑距离就一定是他们之间的长度差,可以先通过add或者delete,然后再通过modify完成,题目最后还要求排序,其实,就是要保证这种方法。


举两个例子吧

abcde和xabzdey 长度差为2,那么先add x, a, 然后modify b, z, d, e, y

xabzdey和abcde 长度差为2, 那么先delete x, a, 然后modify a, b, c, d, e


代码如下:

#include <cstdio>#include <cstring>#include <string>int main(){char s1[10047],s2[10047];int len1,len2,t,i,j,l,k;while(~scanf("%s",s1)){//getchar();scanf("%s",s2);len1 = strlen(s1);len2 = strlen(s2);t = len1 - len2;if(t  == 0 ){for( i = 0 ; i < len2 ; i++ ){printf("m %c\n",s2[i]);}}else if( t > 0){for( i = 0 ; i < t ; i++){printf("d %c\n",s1[i]);}for(i = 0 ; i < len2 ; i++){printf("m %c\n",s2[i]);}}else if( t < 0 ){t = -t;for( i = 0 ; i < t ; i++ ){printf("a %c\n",s2[i]);}for( i = t ; i < len2 ; i++ ){printf("m %c\n",s2[i]);}}}return 0;}



1 0
原创粉丝点击