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. 【动画笔记】数据结构-AVL树的插入操作

    本笔记前置知识: 二叉搜索(排序)树及其插入操作. 本文主要围绕AVL树的平衡因子.纸上做题思路.失衡类型(LL/RR/LR/RL).失衡调整方法.插入后回溯这几部分知识点展开. 注: 本笔记中的平衡 ...

  2. spring源码写注释

    转:https://blog.csdn.net/z_c8819/article/details/105258015 1.从GitHub上下载spring项目 https://github.com/sp ...

  3. *已解决 java写的简单验证码Servlet实践

    目的:java写的简单验证码Servlet实践 总结项目中遇到的问题 提供遇到同样问题的一些(菜鸟的)思路 (代码在最后~) 项目参考:https://www.itdaan.com/blog/2018 ...

  4. VS Code windows系统C/C++环境配置_添加路径

    1.安装cygwin 2.配置VS Code 按ctrl + shift + p ,选择 c/c++:Edit configuration(json) { "configurations&q ...

  5. SpringBoot2.6.x及以上版本整合swagger文档

    SpringBoot的版本更新中引入了一些新的特性,并且Swagger的版本更新也同样引入了很多新的东西,这样就造成了许多配置无法实现一一对应的情况,因此高版本的SpringBoot集成Swagger ...

  6. docker下netcore内存dump

    一般开发阶段可以通过visualstudio来检查程序的内存 .cup等的优化问题.vs下调试=>性能探查器,这里面大有千秋. 但是好多内存问题是经过时间积累下来才暴露出来的,在生产环境中不做不 ...

  7. 通过post请求添加员工信息到数据库

    HMTL部分 js部分

  8. WGCMS 奇迹网站系统 介绍[V2023.2.2]

    智鹏网站系统,请勿用作非法用途 权利和义务: 程序仅限学习技术使用,未经官方许可不得用于商业! 程序售价500元一套,绑定域名,不限制端口.如绑定:xx.com,则www.xx.com.mu.xx.c ...

  9. AttributeError: module 'requests' has no attribute 'get' 报错分析

    这个报错与代码时没有关系 当文件名与调用模块名重合时,系统找不到我们调用的requests模块. 在命名时,我们要注意不要重合.

  10. vue 使用import之后就会报Object(...) is not a function的错

    最近在学习vue,学到了路由,vue-router, 写demo的时候,想引入import VueRotuer from "vue-router",但是添加这句引用浏览器就会报错, ...