String Distance and Transform Process
http://acm.hdu.edu.cn/showproblem.php?pid=1516
Problem Description
Input
Output
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
Delete
Replace ,d
Delete Insert ,a
Insert ,a
Insert ,b
Insert ,a
题意:两个字符串,可以对第一个字符串进行三种操作:1.删除一个字符 2.插入一个字符 3.替换一个字符 使第一个字符串变成第二个字符串,求最少做几次操作
思路:用DP可以求得编辑距离,再从后面开始打印可以打印出路径。构造一个二位数组,dp[i][j] 表示 长度为 i 的 A序列,变成长度为 j 的 B 序列要做的操作数。(注意 i j 可以为0 即 空序列)
代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <algorithm> using namespace std; char str1[],str2[];
int dp[][];
int len1,len2; void solve()
{
int a=len1,b=len2;
int c=dp[a][b];
// printf("a=%d b=%d c=%d\n",a,b,c);
int index=;
while(a>||b>)
{
if(a==&&b>)
{
printf("%d Insert 1,%c\n",++index,str2[b-]);
b--;
continue;
}
else if(a>&&b==)
{
printf("%d Delete %d\n",++index,a);
a--;
continue;
}
else
{
if(c==dp[a-][b-]&&str1[a-]==str2[b-])
{
a--;b--;
}
else if(c==dp[a-][b-]+)
{
printf("%d Replace %d,%c\n",++index,a,str2[b-]);
c--;a--;b--;
}
//后两个else是根据题解改的,不是很理解
else if(c==dp[a][b-]+)
{
printf("%d Insert %d,%c\n",++index,a+,str2[b-]);
c--;b--;
}
else if(c==dp[a-][b]+)
{
printf("%d Delete %d\n",++index,a);
c--;a--;
}
}
}
return ;
} int main()
{
freopen("sample.txt","r",stdin);
while(~scanf("%s %s",str1,str2))
{
getchar();
memset(dp,,sizeof(dp));
len1=strlen(str1);
len2=strlen(str2);
for(int i=;i<=len1;i++)
{
dp[i][]=i;
}
for(int i=;i<=len2;i++)
{
dp[][i]=i;
}
for(int i=;i<=len1;i++)
{
for(int j=;j<=len2;j++)
{
int t=;
if(str1[i-]==str2[j-])
t=;
dp[i][j]=dp[i-][j-]+t;
dp[i][j]=min(dp[i][j],dp[i-][j]+);
dp[i][j]=min(dp[i][j],dp[i][j-]+);
// printf("dp[%d][%d]=%d\n",i,j,dp[i][j]);
}
}
printf("%d\n",dp[len1][len2]);
solve();
}
return ;
}
String Distance and Transform Process的更多相关文章
- 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为各个位置上字符 ...
- 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 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 一位学长的ACM总结(感触颇深)
发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- C# - 多线程 之 Process与Thread与ThreadPool
Process 进程类, // 提供对本地和远程进程的访问,启动/停止本地系统进程 public class Process : Component { public int Id { get; } ...
- ProcessBuilder 、Runtime和Process 的区别
1.版本原因 ProcessBuilder是从java1.5加进来的,而exec系列方法是从1.0开始就有的,后续版本不断的重载这个方法,到了1.5已经有6个之多. 2.ProcessBuilder. ...
随机推荐
- Centos7.4系统 httpd模式搭建文件服务器
环境:服务环境:centos7.4 说明:搭建Apache文件服务器,下载路径为/opt/ymyg(下载路径根据实际需要自己定义) 步骤: 1.安装httpd服务 [root@localhost ...
- [NOI2019]弹跳(KD-Tree)
被jump送退役了,很生气. 不过切了这题也进不了队,行吧. 退役后写了一下,看到二维平面应该就是KD树,然后可以在KD树上做最短路,然后建立堆和KDTree.然后每次更新则是直接把最短路上的节点删掉 ...
- 79.常用的返回QuerySet对象的方法使用详解: filter, exclude,annotate
返回新的QuerySet的常用方法: 1.filter: 将满足条件的数据提取出来,返回一个新的QuerySet 以下所使用的模型article,category,定义模型models.py文件中,示 ...
- 第32&35章 数据库的安装&存储实力的管理
第32章 数据库的安装IO取决于磁盘的个数和接口带宽 版本安装顺序是从低到高存储架构师 第35章 存储实例的管理ASM配置说白了就是ORACLE自己的,不通过操作系统对磁盘进行管理.fdisk -l查 ...
- 201703-2 学生排队 Java
思路: 将需要移动的学生remove后再add 题目中说向前向后移动不会超过人数,也就是不会出现隔着的情况.所以不会越界. import java.util.ArrayList; import jav ...
- try{}catch{}finally{}使用总结
import java.util.Scanner; class MyException extends Exception { public MyException(String Message) { ...
- win10设置开机以及开机无密码验证
1.开机自启动 将程序的exe的快捷方式放入下列文件夹中 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp 2.开机无登录验证 ...
- [CF百场计划]#2 Codeforces Round #618 (Div. 2)
A. Non-zero Description: Guy-Manuel and Thomas have an array \(a\) of \(n\) integers [\(a_1, a_2, \d ...
- zabbix几个配置的关系
- share团队冲刺4
团队冲刺第四天 昨天:进行各种按钮的操作,自定义按钮颜色形状 今天:设置布局,账号密码的输入,选择框 问题:无