http://acm.hdu.edu.cn/showproblem.php?pid=1516

Problem Description

String Distance is a non-negative integer that measures the distance between two strings. Here we give the definition. A transform list is a list of string, where each string, except for the last one, can be changed to the string followed by adding a character, deleting a character or replacing a character. The length of a transform list is the count of strings minus 1 (that is the count of operations to transform these two strings). The distance between two strings is the length of a transform list from one string to the other with the minimal length. You are to write a program to calculate the distance between two strings and give the corresponding transform list.

Input

Input consists a sequence of string pairs, each string pair consists two lines, each string occupies one line. The length of each string will be no more than 80.

Output

For each string pair, you should give an integer to indicate the distance between them at the first line, and give a sequence of command to transform string 1 to string 2. Each command is a line lead by command count, then the command. A command must be

Insert pos,value
Delete pos
Replace pos,value

where pos is the position of the string and pos should be between 1 and the current length of the string (in Insert command, pos can be 1 greater than the length), and value is a character. Actually many command lists can satisfy the request, but only one of them is required.

Sample Input

abcac
bcd
aaa
aabaaaa

Sample Output

 Delete
Replace ,d
Delete Insert ,a
Insert ,a
Insert ,b
Insert ,a

题意:两个字符串,可以对第一个字符串进行三种操作:1.删除一个字符  2.插入一个字符  3.替换一个字符 使第一个字符串变成第二个字符串,求最少做几次操作

思路:用DP可以求得编辑距离,再从后面开始打印可以打印出路径。构造一个二位数组,dp[i][j] 表示 长度为 i 的 A序列,变成长度为 j 的 B 序列要做的操作数。(注意 i j 可以为0 即 空序列)

代码:

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <algorithm> using namespace std; char str1[],str2[];
int dp[][];
int len1,len2; void solve()
{
int a=len1,b=len2;
int c=dp[a][b];
// printf("a=%d b=%d c=%d\n",a,b,c);
int index=;
while(a>||b>)
{
if(a==&&b>)
{
printf("%d Insert 1,%c\n",++index,str2[b-]);
b--;
continue;
}
else if(a>&&b==)
{
printf("%d Delete %d\n",++index,a);
a--;
continue;
}
else
{
if(c==dp[a-][b-]&&str1[a-]==str2[b-])
{
a--;b--;
}
else if(c==dp[a-][b-]+)
{
printf("%d Replace %d,%c\n",++index,a,str2[b-]);
c--;a--;b--;
}
//后两个else是根据题解改的,不是很理解
else if(c==dp[a][b-]+)
{
printf("%d Insert %d,%c\n",++index,a+,str2[b-]);
c--;b--;
}
else if(c==dp[a-][b]+)
{
printf("%d Delete %d\n",++index,a);
c--;a--;
}
}
}
return ;
} int main()
{
freopen("sample.txt","r",stdin);
while(~scanf("%s %s",str1,str2))
{
getchar();
memset(dp,,sizeof(dp));
len1=strlen(str1);
len2=strlen(str2);
for(int i=;i<=len1;i++)
{
dp[i][]=i;
}
for(int i=;i<=len2;i++)
{
dp[][i]=i;
}
for(int i=;i<=len1;i++)
{
for(int j=;j<=len2;j++)
{
int t=;
if(str1[i-]==str2[j-])
t=;
dp[i][j]=dp[i-][j-]+t;
dp[i][j]=min(dp[i][j],dp[i-][j]+);
dp[i][j]=min(dp[i][j],dp[i][j-]+);
// printf("dp[%d][%d]=%d\n",i,j,dp[i][j]);
}
}
printf("%d\n",dp[len1][len2]);
solve();
}
return ;
}
 

String Distance and Transform Process的更多相关文章

  1. Codeforces CF#628 Education 8 C. Bear and String Distance

    C. Bear and String Distance time limit per test 1 second memory limit per test 256 megabytes input s ...

  2. CF 628C --- Bear and String Distance --- 简单贪心

    CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...

  3. Educational Codeforces Round 8 C. Bear and String Distance 贪心

    C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...

  4. codeforces 628C C. Bear and String Distance

    C. Bear and String Distance time limit per test 1 second memory limit per test 256 megabytes input s ...

  5. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  6. 一位学长的ACM总结(感触颇深)

    发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...

  7. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  8. C# - 多线程 之 Process与Thread与ThreadPool

    Process 进程类, // 提供对本地和远程进程的访问,启动/停止本地系统进程 public class Process : Component { public int Id { get; } ...

  9. ProcessBuilder 、Runtime和Process 的区别

    1.版本原因 ProcessBuilder是从java1.5加进来的,而exec系列方法是从1.0开始就有的,后续版本不断的重载这个方法,到了1.5已经有6个之多. 2.ProcessBuilder. ...

随机推荐

  1. Android进阶——多线程系列之Semaphore、CyclicBarrier、CountDownLatch

    今天向大家介绍的是多线程开发中的一些辅助类,他们的作用无非就是帮助我们让多个线程按照我们想要的执行顺序来执行.如果我们按照文字来理解Semaphore.CyclicBarrier.CountDownL ...

  2. git使用散记

    1.从远程clone一个项目 git clone ‘项目地址’ //clone项目地 git checkout -b dev origin/dev //远程已有dev分支,新建本地dev分支与远程相对 ...

  3. gabor滤波器

    https://blog.csdn.net/u013709270/article/details/49642397 https://github.com/xuewenyuan/Gabor_Visual ...

  4. redis--主从复制(读写分离)

    应用程序对服务器大量的读写,服务器很可能会宕机,导致数据丢失.为了解决这一问题就有了主从复制. 作用: 1:防止数据丢失 2:提高系统的吞吐量 主从复制:从服务器复制主服务器中的数据. 读写分离:应用 ...

  5. find_element_by_xpath()的6种方法

    Xpath (XML Path Language),是W3C定义的用来在XML文档中选择节点的语言 一:从根目录/开始 有点像Linux的文件查看,/代表根目录,一级一级的查找,直接子节点,相当于cs ...

  6. 解决configure: error: C++ compiler cannot create executables问题

    参考 yum install gcc gcc++ 呵呵,这样的话还是有组件没有安装完整的.再执行一下这个命令就可以解决问题. yum install gcc gcc-c++ gcc-g77

  7. nginx重写常用写法

    1.将http协议重写成https协议: (用户用http进行访问,但后端是https),则可添加80 http端口监听,然后进行https rewrite; server {     listen ...

  8. DRF框架之DRF的引入

    DRF框架是python_web中采用前后端分离开发模式的框架,其处理JSON数据是最快的. 通过DRF框架,我们后端程序员只需要拼接并响应JSON数据即可,并且数据复用性高适用于浏览器端.APP端等 ...

  9. 学会拒绝,是一种智慧——OO电梯章节优化框架的思考

    在本章的三次作业里,每次作业我都有一个主题,分别是:托盘型共享数据.单步电梯运行优化.多部电梯运行优化,因而电梯优化实际是第二.三次作业.虽然后两次作业从性能分上看做得还不错,但阅读其他大佬博客,我深 ...

  10. vue的选项卡功能

    选项卡:点击不同的按钮会显示不同的内容 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...