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;
}


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

  1. String Distance and Transform Process

    http://acm.hdu.edu.cn/showproblem.php?pid=1516 Problem Description String Distance is a non-negative ...

  2. 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 ...

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

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

  4. HDU 3374 String Problem (KMP+最大最小表示)

    HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory ...

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

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

  6. 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 ...

  7. hdu 1039 (string process, fgets, scanf, neat utilization of switch clause) 分类: hdoj 2015-06-16 22:15 38人阅读 评论(0) 收藏

    (string process, fgets, scanf, neat utilization of switch clause) simple problem, simple code. #incl ...

  8. hdu 4712 Hamming Distance(随机函数暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  9. HDU 3374 String Problem(KMP+最大/最小表示)

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  10. hdu 4712 Hamming Distance 随机

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

随机推荐

  1. 元数据库 information_schema.tables

    转  https://www.cnblogs.com/ssslinppp/p/6178636.html 1.information_schema数据库 对于mysql和Infobright等数据库,i ...

  2. Idea移除和删除模块

    移除:右键模块-remove moduel 删除:在移除操作后 右键模块-delete 然后删除项目pom文件里面的<moduel>

  3. 4.12 疫情数据可视化 毕设(初稿版 crud+可视化echarts

    4.22 完成地图 数据可视化~~~  599x150 解决不显示图片的问题 参考文档 https://blog.csdn.net/qq_51917985/article/details/121380 ...

  4. 【翻译】API 链接与键:为什么应该使用链接而不是键来表示 API 中的关系

    翻译自原文: https://cloud.google.com/blog/products/application-development/api-design-why-you-should-use- ...

  5. Eureka、Consul、Zookeeper注册中心总结

    组件名 编写语言 CAP 服务健康检查 对外暴露接口 Springcloud集成 Eureka Java AP 可配支持(安全机制) Http √ Consul Go CP 支持 Http/DNS √ ...

  6. Excel文件 利用MySQL/Python 实现自动处理数据的功能

    目录 问题描述: 解决方案: 一.SQL查询 二.SQL.python处理 三.python处理 四.优化python处理 1.手动执行代码 2.开机自动执行代码 对比四种方案: 总结: 问题描述: ...

  7. 【KAWAKO】iphone13pro开箱流程

    目录 全程录像 检查包装盒 检查包装盒内物品 检查各种码 拆封 激活 激活之后 检查屏幕 检查其它功能 贴膜(选) References 全程录像 如果你觉得你所购买的平台 (比如某ABB格式名字的平 ...

  8. Revit如何给模型绑定动画的教程

    推荐:将 NSDT场景编辑器 加入你的3D开发工具链. Revit模型完成后,为了展示成果,有时需要做动画,本文章将教大家如何在3dsmax中给塔吊族模型绑定旋转动画,并导入到Lumion当中使用. ...

  9. “堆内存持续占用高 且 ygc回收效果不佳” 排查处理实践

    作者:京东零售 王江波 说明:部分素材来源于网络,数据分析全为真实数据. 一. 问题背景 自建的两套工具,运行一段时间后均出现 内存占用高触发报警,频繁young gc且效果不佳.曾经尝试多次解决,因 ...

  10. Vue 组件VueComponent中_ _proto_ _ 原型对象的指向(指向Vue的原型对象 _ _proto_ _)

    1.VueComponent.prototype.__proto__ === Vue.prototype 2.让组件实例对象(vc)可以访问到Vue原型上的属性.方法 图片如下: 案例: Vue.pr ...