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
3
1 Delete 1
2 Replace 3,d
3 Delete 4
4
1 Insert 1,a
2 Insert 2,a
3 Insert 3,b
4 Insert 7,a

代码
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char s1[1005], s2[1005];
int dp[1005][1005];
int main()
{
int len1, len2, i, j, t;
while (~scanf("%s%s", s1, s2)){
len1 = strlen(s1); len2 = strlen(s2);
for (i = len1; i >= 1; i--)
s1[i] = s1[i - 1];
for (i = len2; i >= 1; i--)
s2[i] = s2[i - 1];
for (i = 0; i <= len1; i++)
for (j = 0; j <= len2; j++)
{
if (i == 0 && j == 0) dp[i][j] = 0;
else if (i == 0) dp[i][j] = j;
else if (j == 0) dp[i][j] = i;
else {
if (s1[i] == s2[j]) dp[i][j] = dp[i - 1][j - 1];
else dp[i][j] = dp[i - 1][j - 1] + 1;
dp[i][j] = min(dp[i][j], min(dp[i - 1][j], dp[i][j - 1]) + 1); }
}
printf("%d\n", dp[len1][len2]);
t = 0;
i = len1;
j = len2;
while (i > 0 || j > 0)
{
if (s1[i] == s2[j] && dp[i][j] == dp[i - 1][j - 1]){
i--;
j--;
continue; }
t++;
printf("%d ", t);
if (j > 0 && dp[i][j] == dp[i][j - 1] + 1){
printf("Insert %d,%c\n", i + 1, s2[j]);
j--; }
else if (i > 0 && dp[i][j] == dp[i - 1][j] + 1){
printf("Delete %d\n", i);
i--; }
else if (dp[i][j] == dp[i - 1][j - 1] + 1){
printf("Replace %d,%c\n", i, s2[j]);
i--;
j--; }
}
}
system("pause");
return 0;
}


最新文章

  1. 学习ASP.NET Core, 怎能不了解请求处理管道[5]: 中间件注册可以除了可以使用Startup之外,还可以选择StartupFilter
  2. mac安装虚拟机
  3. python调用NLPIR - ICTCLAS2013实现中文分词
  4. 按键消抖-----verilog
  5. 《ASP.NET1200例》各种类型文件汇总
  6. 第二篇,MVC框架
  7. HUST 1010 The Minimum Length(KMP,最短循环节点,即i-Next[i])
  8. Android发送请求到不同的Servlet,但都是一个Servlet处理
  9. Struts2 文件上传
  10. 一周一话题之四(JavaScript、Dom、jQuery全面复习总结&lt;Dom篇&gt;)
  11. winform 加密 解密 分类: WinForm 2014-05-16 15:05 400人阅读 评论(0) 收藏
  12. 【Android进阶】关于PagerAdapter的使用方法的总结
  13. HTML5之2D物理引擎 Box2D for javascript Games 系列 第三部分之创建图腾破坏者的关卡
  14. PHP把采集抓取网页的html中的的&amp;nbsp;去掉或者分割成数组
  15. python基础一 ------可迭代类型的连接
  16. 《JavaScript Dom 编程艺术》读书笔记-第9章
  17. alpha冲刺(5/10)
  18. xfsdump 备份文件系统
  19. 网络抓包神器-Charles使用指南
  20. Steam安装Google Earth VR

热门文章

  1. CentOS7登录到控制台后无网络
  2. python爬虫学习——列表
  3. P19_数据绑定
  4. 记录一次vue部署docker步骤
  5. echarts使用dataset数据集创建单轴散点图
  6. vue3.0+echart可视化
  7. Cesium计算多边形面积(十一)
  8. dvwa靶场
  9. 网关与网络地址(网络号)以及IP地址、广播地址
  10. 跳板攻击之:EarthWorm代理转发