HDU 2895 Edit distance(数学+模拟)
2014-05-02 15:33
369 查看
转载请注明出处:http://blog.csdn.net/u012860063
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2895
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
Sample Output
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
代码如下:
题目链接: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
abcde xabzdey
Sample Output
a x a a m b m z m d m e m 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; }
相关文章推荐
- hdu 4611 Balls Rearrangement(数学:推理+模拟)
- hdu 5373 The shortest problem(模拟、数学规律)
- HDU 1013 Digital Roots 模拟//数学题
- HDU 4611 (2013多校联赛1001 数学+模拟)
- HDU 4814 Golden Radio Base 数学 模拟 或者乱搞。。。。
- HDU 2932 Extraordinarily Tired Students(数学 & 模拟)
- 2017 Multi-University Training Contest - Team 9 1004&&HDU 6164 Dying Light【数学+模拟】
- HDU 1099 Lottery (数学问题模拟)
- HDU - 4007 Dave (模拟&技巧)数学几何正方形最多可圈几个点
- hdu 1210 简单数学模拟
- 7_4_E题 Edit distance题解 [hdu 2895] (贪心)
- hdu 4814 Golden Radio Base(数学模拟)
- HDU 2895 Edit distance(贪心)
- hdu 5491 The Next(数学模拟)
- HDU-1006 Tick and Tick(模拟 + 区间交集 + 数学)
- 数学模拟 HDU-4814
- HDU 1988 & ZOJ 2991 Flipping Burned Pancakes(数学啊+模拟)
- HDU 1111 - Secret Code (DFS + 一点数学 + 模拟)
- hdu_1049 Climbing Worm(数学,模拟)
- HDU 5019 Revenge of GCD(数学)