原题: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): 314 Accepted Submission(s): 144
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.
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.
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.
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
题意:输入2个字符串并比较字符串长度,如果上面的字符串大于下面的字符串就输出上面字符串的前几个(并在每个字符前面加一个d)直到字符串长度相同,之后输出下面的字符串(并在每个字符前面假一个m);
反之,如果下面的大于上面的这输出下面字符串前几个(并在每个字符前面加一个a)直到字符串长度相同,之后输出下面的字符串(并在每个字符前面假一个m)。
如下AC代码:
#include <stdio.h>
#include <string.h>
/*
author:YangSir
time:2014/5/2
*/
int main()
{
int i,la,lb;
char a[10002],b[10002];
while(~scanf("%s%s",a,b))
{
la=strlen(a);
lb=strlen(b);
if(la<=lb)
{
for(i=0;i<lb;i++)
{
if(i<lb-la)
printf("a %c\n",b[i]);
else
printf("m %c\n",b[i]);
}
}
else
{
for(i=0;i<la-lb;i++)
{
printf("d %c\n",a[i]);
}
for(i=0;i<lb;i++)
{
printf("m %c\n",b[i]);
}
}
}
return 0;
}