HDU 2895 字符串处理(看懂就简单)

原题: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. 


 

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
 
题意:输入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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值