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. 删除所有的docker容器和镜像(转载)

    列出所有的容器 ID docker ps -aq 停止所有的容器 docker stop $(docker ps -aq) 删除所有的容器 docker rm $(docker ps -aq) 删除所 ...

  2. Angular表单 (一)表单简介

    Angular 表单 angular提供了两种不同的方法来通过表单处理用户输入:响应式表单和模板驱动表单.二者都从视图中捕获用户输入事件.验证用户输入.创建表单模型.修改数据模型,并提供跟踪这些更改的 ...

  3. 腾讯大佬告诉你,写Python到底用什么IDE合适

    不管你是 Python 新手还是老鸟,肯定纠结过一个问题: 到底用什么编辑器写 Python 代码好? 为此,我们调查了数十位鹅厂程序猿们爱用的 Python IDE,从他们对每款编辑器的看法中,也许 ...

  4. JVM探秘:jstat查看JVM统计信息

    本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. jstat命令用来查看JVM统计信息,可以查看类加载信息.垃圾收集的信息.JIT编译信 ...

  5. python课后练习当前目录下有一个文件名为score3.txt的文本文件, 存放着某班学生的学号和其两门专业课的成绩。

    题目: 当前目录下有一个文件名为score3.txt的文本文件, 存放着某班学生的学号和其两门专业课的成绩.分 别用函数实现以下功能: (1) 定义函数function1,计算每个学生的平均分(取 整 ...

  6. maven打包 invalid entry size Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.14.RELEASE:repackage (default) on project

    打包失败,但是不知是具体是什么引起得,使用mvn -e clean package,定位到报错得代码 在定位到代码,打上断点,使用maven 打包debug模式 找到dubbo.properties, ...

  7. tensorflow 分布式训练

    TF实现分布式流程 1.创建集群 ClusterSpec & Server cluster = tf.train.ClusterSpec({"ps": ps_hosts, ...

  8. 经理人和app开发者大打出手,说明这个市场已经畸形变态?

    日前,一件民生事件在网络上广为流传,成为人们热议的话题:中国平安的产品经理向app开发者提了一个需求,要求用户app的主题颜色能根据手机外壳自动调整,可能是开发人员觉得这个要求太不合理,而且感到十分绝 ...

  9. 35. docker swarm dockerStack 部署 投票应用

    1. 编写 docker-compose.yml # docker-compose.yml version: "3" services: redis: image: redis:a ...

  10. h5-过度

    1.过度的基本介绍及写法 .div{ width: 200px; height: 200px; background-color: red; position: absolute; left: 100 ...