hdu 1516 String Distance and Transform Process
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.
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的更多相关文章
- String Distance and Transform Process
http://acm.hdu.edu.cn/showproblem.php?pid=1516 Problem Description String Distance is a non-negative ...
- 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 ...
- CF 628C --- Bear and String Distance --- 简单贪心
CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...
- HDU 3374 String Problem (KMP+最大最小表示)
HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others) Memory ...
- Educational Codeforces Round 8 C. Bear and String Distance 贪心
C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...
- 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 ...
- 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 ...
- hdu 4712 Hamming Distance(随机函数暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
- HDU 3374 String Problem(KMP+最大/最小表示)
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 4712 Hamming Distance 随机
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
随机推荐
- SQL优化的七个方面
SQL优化的七个方面 1. 创建索引 禁止给表中每一列都建立单独索引 每个Innodb表都必须有一个主键 要注意组合索引的字段顺序 优先考虑覆盖索引 避免使用外键约束 2. 避免索引失效 失效场景: ...
- Vue08 数据代理
1 说明 所谓"数据代理",是指 通过一个对象代理对另一个对象的属性进行读或写操作. 2 简单示例 2.1 代码 let obj = {x:100}; let obj2 = {y: ...
- Jest + React 单元测试最佳实践
我们是袋鼠云数栈 UED 团队,致力于打造优秀的一站式数据中台产品.我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值. 前言 单元测试是一种用于测试"单元"的软件测试方 ...
- java并发AQS中应用:以acquire()方法为例来分析线程间的同步与协作
谈到java中的并发,我们就避不开线程之间的同步和协作问题,谈到线程同步和协作我们就不能不谈谈jdk中提供的AbstractQueuedSynchronizer(翻译过来就是抽象的队列同步器)机制: ...
- STM32F4跳转函数
JMP2APP void JMP2APP(void) { pFunction Jump_To_Application; uint32_t JumpAddress; if (((*(__IO uint3 ...
- FCoE测试重启调试记录
环境 CPU:Phytium,S2500/64 C00 内核版本:4.19.90-25.10 网讯网卡:txgbe 共两台设备,光纤直连 复现步骤 设备A.B分别执行以下操作,即可复现 modprob ...
- redis(1)NoSQL数据库简介
1.1 技术发展 redis是用来解决性能问题的数据库 技术的分类: 解决功能性问题:Java.Jsp.RDBMS.Tomcat.HTML.Linux.JDBC.SVN 解决扩展性问题:Struts. ...
- JZOJ 4213. 【五校联考1day2】对你的爱深不见底
题目 思路 结论题,我不会证明: 找到第一个 \(|S_n| \leq m + 1\),那么答案就是 \(m - |S_{n-2}|\) 证明?我说了我不会,就当结论用吧 这已经很恶心了 然而这题还要 ...
- Canvas:绘制失败的问题
beginPath 绘制路径必须添加 beginPath().它标志着一个画笔在画布中哪个地方开始画起.没有它,新起的画笔位置必定与上一次画笔结束的位置相连. // 第一个半圆 ctx.arc(60, ...
- Java第三讲动手动脑
1 以上代码无法通过编译主要是由于在Foo类中自定义了有参的构造函数,系统不在提供默认的构造函数(无参),而在上述的引用中并没有提供参数导致无法通过编译. 2. 运行结果 由运行结果分析可知,在运行时 ...