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. <asp:FileUpload>控件 获取不到文件名

    <asp:FileUpload>控件 放在了<asp:UpdatePanel>控件中会导致获取不到文件名.

  2. vue过渡效果

    vue过渡效果. <transition name='slide-fade'> <div class="top" @click='gotoTop' v-if='s ...

  3. Oracle 数据库名、实例名、Oracle_SID

    本文参考自ORACLE 数据库名.实例名.ORACLE_SID的区别,纯属读书笔记,加深记忆 在ORACLE7.8数据库中只有数据库名(db_name)和数据库实例名(instance_name).在 ...

  4. MySQL使用内置函数来进行模糊搜索(locate()等)

    常用的一共有4个方法,如下: 1. 使用locate()方法 1.1.普通用法: SELECT `column` from `table` where locate('keyword', `condi ...

  5. CentOS 7下使用yum安装MySQL5.7

    1.卸载 1.1先停掉mysql进程,没有安装过的可以直接跳过 pkill - mysqld rpm -qa|grep -i mysql 1.2用命令 yum -y remove -.el7.x86_ ...

  6. bzoj 4161: Shlw loves matrixI

    Description 给定数列 {hn}前k项,其后每一项满足 hn = a1h(n-1) + a2h(n-2) + ... + ak*h(n-k) 其中 a1,a2...ak 为给定数列.请计算 ...

  7. [转]AngularJS 实现 Table的一些操作(示例大于实际)

    本文转自:http://www.cnblogs.com/lin-js/p/linJS.html <!DOCTYPE html> <html> <head> < ...

  8. nodejs中引入art-template模板

    使用Webstorm创建nodejs express应用时,默认使用的是jade或者ejs模板,对于不习惯这两种模板语法的人来说确实不是很方便.没关系,这里我们使用art-template模板引擎,使 ...

  9. Windows下的代码注入

    木马和病毒的好坏很大程度上取决于它的隐蔽性,木马和病毒本质上也是在执行程序代码,如果采用独立进程的方式需要考虑隐藏进程否则很容易被发现,在编写这类程序的时候可以考虑将代码注入到其他进程中,借用其他进程 ...

  10. csharp: using OleDb Getting the identity of the most recently added record

    /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString&quo ...