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. ...
随机推荐
- mysql 如何删除数据库中所有的表
SELECT concat('DROP TABLE IF EXISTS ', table_name, ';')FROM information_schema.tablesWHERE table_sch ...
- ETL优化
ETL优化 Extract.Transform.Load,对异构数据源进行数据处理. 设立基线标准,根据硬盘.网络传输速度,多测测量得到数据量(m)/时间(s)的比值,找线性关系.建立基线作为调试和优 ...
- P1426 小鱼会有危险吗
题解: 在测试数据里有一个是临界值问题,探测范围是闭区间 #include<stdio.h>int main(){ double s,x; //注意:此 ...
- 51nod 1284:2 3 5 7的倍数 容斥原理
1284 2 3 5 7的倍数 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 给出一个数N,求1至N中,有多少个数不是2 3 5 7的倍数. 例如N ...
- 干货|CVE-2019-11043: PHP-FPM在Nginx特定配置下任意代码执行漏洞分析
近期,国外安全研究员Andrew Danau,在参加夺旗赛(CTF: Capture the Flag)期间,偶然发现php-fpm组件处理特定请求时存在缺陷:在特定Nginx配置下,特定构造的请求会 ...
- 干货 | 京东云托管Kubernetes集成镜像仓库并部署原生DashBoard
在上一篇"Cloud Native 实操系列"文章中,我们为大家介绍了如何通过京东云原生容器实现Eureka的部署(
- PAT Advanced 1102 Invert a Binary Tree (25) [树的遍历]
题目 The following is from Max Howell @twitter: Google: 90% of our engineers use the sofware you wrote ...
- 模拟jenkins通过shell给ansible传入变量
jenkins.sh #!/bin/bash name1='robin h h li' age1='11' declare -A dic dic=( [name1]="${name1}&qu ...
- Aras Innovator客户端批量下载关联文件
<button onclick="btnDownload();" id="downfilebtn">批量下载关联文件</button> ...
- GTX 1080显卡出错
NVRM: RmInitAdapter failed! (0x26:0xffff:1097) NVRM: rm_init_adapter failed for device bearing minor ...