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. python----linux下简单的排序

    1.选择排序:把一个数与余下所有的数排序,最小的排到最前面 [root@besttest liyn_test]# cat test.py #! /usr/bin/python a=[,,,] ,len ...

  2. POJ 1995:Raising Modulo Numbers 快速幂

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5532   Accepted: ...

  3. 题解 Luogu P2499: [SDOI2012]象棋

    关于这道题, 我们可以发现移动顺序不会改变答案, 具体来说, 我们有以下引理成立: 对于一个移动过程中的任意一个移动, 若其到达的位置上有一个棋子, 则该方案要么不能将所有棋子移动到最终位置, 要么可 ...

  4. 吴裕雄--天生自然MySQL学习笔记:MySQL PHP 语法

    MySQL 可应用于多种语言,包括 PERL, C, C++, JAVA 和 PHP. 在这些语言中,Mysql在PHP的web开发中是应用最广泛. PHP提供了多种方式来访问和操作Mysql数据库记 ...

  5. 吴裕雄--天生自然 JAVASCRIPT开发学习: HTML DOM - 改变CSS

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 使用linux服务器安装wordpress博客详细教程

    前言 最近读了<软技能:代码之外的生存指南>,这本书给了我很大的启示.之前虽然知道作为一个程序员,应该拥有自己的博客,以便于提升自己的知名度,但是并没有了解的过于详细.这本书描写博客的作用 ...

  7. Java中常用的API(一)——Object

    概述 如果要问Java为什么是用起来非常舒服的语言,那很大一部分的功劳就是JavaAPI的.API定义了许多封装好的类和方法供我们使用,来处理特定的问题,所以学习常用的API是非常重要的. 同时,面向 ...

  8. TCP协议的学习

    1.关于TCP理解的重点(TCP协议可以理解为就是一段代码) (1).TCP协议工作在传输层,对上服务socket接口,对下调用IP层 (2).TCP协议面向连接,通信前必须先3次握手建立连接关系后才 ...

  9. [CF百场计划]Codeforces Round #617 (Div. 3)

    A. Array with Odd Sum Description You are given an array \(a\) consisting of \(n\) integers. In one ...

  10. MySQL主主、主从、从从配置文件

    主配置文件: [root@sun01 ~]# more /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql. ...