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) ...
随机推荐
- Vue19 常用指令及自定义指令
部分转自:https://blog.csdn.net/weixin_58032613/article/details/122759818 1 常用指令 1) v-bind 单向数据绑定 https:/ ...
- Activiti02流程基本功能使用
主要分为一下几个步骤: 1.画图 2.部署流程-把图的信息转入到数据表格中 3.创建流程实例-开始一个流程-实际发起了一个流程 4.执行任务:获取任务+完成任务 1.画图 画了一个简单的流程图,图形文 ...
- UEFI引导安装UBUNUT
1.引导方式一定要选UEFI,否则一些显卡驱动将不能安装 2.安装的时候,要在第四个界面,也就是选择覆盖安装还是保留双系统的那个界面,选择其他,一定要自己分区 3.分区: 4.一共5个重要分区: 1. ...
- 微信小程序【关于地址信息的接入以及自动选择当前位置】
在做收货地址的时候,通常会让用户填写或者打开地图选择收获地址,此时就需要用到微信提供的地址API,在使用地址API的时候需要注册对应的API,而且地址API会经常调整,需要关注官方公告,下面就是关于地 ...
- JZOJ 4314. 【NOIP2015模拟11.4】老司机
题目 思路 大意是构造一个数组使它做 \(01\) 背包能表示出所有给定的数 那就暴力枚举每个位置填什么 直到它能表示出所有给定的数 为了保证时间复杂度 我们考虑一个二进制数 \(s\) 表示能构造出 ...
- group by 、concat_ws()、 group_caoncat()的使用
group系列 之前觉得这里简单不需要再进行总结了.后来发现还是需要总结巩固一下,还是有一些方法之类的之前未使用过.这里来重新整理,记录一下. group by 将表中的数据根据某个条件进行分组. 比 ...
- 权限维持之:SID History 域控权限维持
目录 1 SID 作用 2 利用 SID History 操作过程 3 SID History 权限维持的防御 1 SID 作用 每个用户都有自己的SID,SID的作用主要是跟踪安全主体控制用户连 ...
- 网络配置:Netplan
Netplan 新出的Ubuntu服务器18.04版本修改了IP地址配置程序, Ubuntu和Debian的软件架构师删除了以前的ifup/ifdown命令和/etc/network/interfac ...
- C# 通过反射获取类字段名和值并加入到字典中(包含递归获取)
//测试类 public class HKAddvisitor1{ public string code { set; get; } public string msg { set; ge ...
- Flutter:StatefulWidget 怎么传参
定义好一个有状态(或无状态)的组件之后,需要为其传递一些参数,希望组件能够更加灵活使用.那么,在 Flutter 中如何为其传递参数呢? 以下是 StatefulWidget 传递值的步骤,一共三步: ...