hdu 1516(编辑距离+记录路径)
解题思路:这道题的基本模型就是编辑距离的模型,只是多了一个路径记录的过程。
学习一下:http://www.cnblogs.com/biyeymyhjob/archive/2012/09/28/2707343.html
最开始把问题搞错了,以为是两个串都可以做修改,无论我怎么想都不通。
回到这个题目上,这道题和最长公共子序列很相似,思路可以说是一样的,包括记录路径。
其实也就是根据递推数组的结果来判断。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;const int maxn = 85;
char A[maxn],B[maxn];
int dp[maxn][maxn],len1,len2;void path()
{int tmp, i = len1, j = len2;int step = 0;while(i >= 1 || j >= 1){if(A[i-1] == B[j-1]) tmp = 0;else tmp = 1;if(dp[i][j] == dp[i-1][j-1] + tmp && i >= 1 && j >= 1){if(tmp)printf("%d Replace %d,%c\n",++step,i,B[j-1]);i--, j--;}else if(dp[i][j] == dp[i-1][j] + 1 && i >= 1){printf("%d Delete %d\n",++step,i);i--;}else if(dp[i][j] == dp[i][j-1] + 1 && j >= 1){printf("%d Insert %d,%c\n",++step,i+1,B[j-1]);j--;}}
}int main()
{while(scanf("%s %s",A,B)!=EOF){getchar();len1 = strlen(A);len2 = strlen(B);memset(dp,0,sizeof(dp));for(int i = 0; i <= len1; i++)dp[i][0] = i;for(int i = 0; i <= len2; i++)dp[0][i] = i;for(int i = 1; i <= len1; i++)for(int j = 1; j <= len2; j++){int tmp = min(dp[i][j-1],dp[i-1][j]) + 1;int d = A[i-1] == B[j-1] ? 0 : 1;dp[i][j] = min(tmp,dp[i-1][j-1]+d);}printf("%d\n",dp[len1][len2]);path();}return 0;
}
hdu 1516(编辑距离+记录路径)相关推荐
- hdu 1026 bfs+记录路径
题意:从0,0点出发到n-1,m-1点,路上的数字代表要在这个点额外待多少秒,求最短的路 递归输出路径即可 1 #include<cstdio> 2 #include<iostrea ...
- FatMouse's Speed hdu 1160(动态规划,最长上升子序列+记录路径)
http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意:现给出老鼠的体重与速度,要求你找出符合要求的最长子序列. 要求是 W[m[1]] < ...
- HDU ACM 3986 Harry Potter and the Final Battle(邻接表实现最短路dijkstra堆优化记录路径 + 枚举最短路上每条边)...
http://acm.hdu.edu.cn/showproblem.php?pid=3986 题意: 从起点1 到 终点n,删除图中任意一条边求最短路的最坏情况. n --表示有n个点 m --边数 ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- 【HDU - 1026 】Ignatius and the Princess I (bfs + 记录路径)
题干: The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pr ...
- ☆【UVA - 624 】CD(dp + 0-1背包 + 记录路径)
题干: You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music ...
- 【POJ - 3310】Caterpillar(并查集判树+树的直径求树脊椎(bfs记录路径)+dfs判支链)
题干: An undirected graph is called a caterpillar if it is connected, has no cycles, and there is a pa ...
- (dijkstra记录路径)find the longest of the shortest
Marica对Mirko很生气,因为他找到了一个新的女朋友,她想报仇.由于她不住在同一个城市,她开始为长途旅行做准备.我们知道每条路从一个城市到另一个城市需要多少分钟. 米尔科在车里无意中听到其中一条 ...
- 郊区春游(状压DP水题)+ 记录路径
题目链接: https://ac.nowcoder.com/acm/problem/16122 题目大意: 中文 具体思路: 首先对全图跑一遍floyed,然后dp[i][j]表示第i个状态在j点停下 ...
最新文章
- postman 请求 页面出现 Could not get any response 解决方法
- 一分钟了解阿里云产品:容器服务概述
- 程序员面试题精选100题(53)-C++/C#面试题(2)
- Linux最小体积mysql安装_Linux下安装MySQL以及一些小坑
- 三十四、数据仓库的建模
- php主机安装v2,RackTables 安装教程Installation GuideV2
- Eclipse相关快捷键
- 关于uint32_t uint8_t uint64_t 的问题
- JS Addition
- 代数式对应的C语言表达式不等价的是( ),C语言重修复习题分析.doc
- 怎么理解anchor
- oracle 循环select查询的结构集,执行insert到指定表保存
- Common lisp之加载方式(一)
- 有哪些不讲武德的国外计算机学习资源?
- centos7学习笔记-安装配置apache
- excel数据库_EXCEL数据库函数dcount、dcounta
- 数学建模-灰色预测模型基本原理及其编程实现
- 乐鑫Esp32-S2学习之旅② ESP32-S2 以 I2C 驱动 SHT20 获取温湿度数据,代码开源!
- 移植AT91Bootstrap1.15
- 10月各国最新签证及入境政策汇总