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来代替。

 #include<stdio.h>
#include<string.h>
#define MAXN 10000
char s1[MAXN],s2[MAXN];
int main()
{
int n1,n2,n,i;
while(scanf("%s",s1)!=EOF)
{
scanf("%s",s2);
n1=strlen(s1);
n2=strlen(s2);
n=n2-n1;
if(n>)
{
for(i=;i<n2;i++)
{
if(n>)
{
printf("a %c\n",s2[i]);
n--;
}
else printf("m %c\n",s2[i]);
}
}
else if(n==)
{
for(i=;i<n2;i++)
printf("m %c\n",s2[i]);
}
else{
for(i=;i<n1;i++)
{
if(n<)
{
printf("d %c\n",s1[i]);
n++;
}
}
for(i=;i<n2;i++)
printf("m %c\n",s2[i]);
}
}
return ;
}

HDU ACM 2895-Edit distance的更多相关文章

  1. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  2. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  3. Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  4. 编辑距离——Edit Distance

    编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...

  5. LintCode Edit Distance

    LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...

  6. stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)

    I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...

  7. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  8. hdu acm 1028 数字拆分Ignatius and the Princess III

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  9. 动态规划 求解 Minimum Edit Distance

    http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...

随机推荐

  1. 在Eclipse添加Android兼容包( v4、v7 appcompat )[转]

    昨天添加Android兼容包,碰到了很多问题,在这里记录一下,让后面的路好走. 如何选择兼容包, 请参考Android Support Library Features(二) 一.下载Support ...

  2. php将“\\”转换成“\”的例子

    str_replace('\\\\', '\\', $url);

  3. Linux 命令 -- chmod

    chmod命令用来变更文件或目录的权限.在UNIX系统家族里,文件或目录权限的控制分别以读取.写入.执行3种一般权限来区分,另有3种特殊权限可供运用.用户可以使用chmod指令去变更文件与目录的权限, ...

  4. Intellij IDEA自定义类模板和方法模板

    以Intellij IDEA 2017.3.5为例 定义类模板 依次打开File->Settings->File and Code Templates->Files, 选择class ...

  5. Scrapy框架学习(四)爬取360摄影美图

    我们要爬取的网站为http://image.so.com/z?ch=photography,打开开发者工具,页面往下拉,观察到出现了如图所示Ajax请求, 其中list就是图片的详细信息,接着观察到每 ...

  6. net 记录controller Action耗时

    可能有些时候需要记录Action的执行时间来优化系统功能,这时可以用过滤器来实现 第1个例子 using System; using System.Diagnostics; using System. ...

  7. 创建WPF用户控件

    wpf用户自定义控件和winform创建方法类似,这里先纠正一个误区,就是有很多人也是添加,然后新建,然后是新建用户控件库,但是为什么编译好生成后Debug目录下还是只有exe文件而没有dll文件呢? ...

  8. 不能修改列 "。。",因为它是计算列,或者是 UNION 运算符的结果。

    修改Mapping this.Property(t => t...).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotatio ...

  9. 如何通过 PHP 获取 Azure Active Directory 令牌

    在调用 Azure Rest API 时,如果是属于 Azure Resource Manager 的 API,则需要使用 Azure Active Directory (Azure AD)认证获取令 ...

  10. JDK12 concurrenthashmap源码阅读

           本文部分照片和代码分析来自文末参考资料        java8中的concurrenthashmap的方法逻辑和注解有些问题,建议看最新的JDK版本        建议阅读 concu ...