POJ_3356——最短编辑距离,动态规划
Description
Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given below:
- Deletion: a letter in x is missing in y at a corresponding position.
- Insertion: a letter in y is missing in x at a corresponding position.
- Change: letters at corresponding positions are distinct
Certainly, we would like to minimize the number of all possible operations.
Illustration
A G T A A G T * A G G C
| | | | | | |
A G T * C * T G A C G CDeletion: * in the bottom line Insertion: * in the top line Change: when the letters at the top and bottom are distinct
This tells us that to transform x = AGTCTGACGC into y = AGTAAGTAGGC we would be required to perform 5 operations (2 changes, 2 deletions and 1 insertion). If we want to minimize the number operations, we should do it like
A G T A A G T A G G C
| | | | | | |
A G T C T G * A C G C
and 4 moves would be required (3 changes and 1 deletion).
In this problem we would always consider strings x and y to be fixed, such that the number of letters in x is m and the number of letters in y is n where n ≥ m.
Assign 1 as the cost of an operation performed. Otherwise, assign 0 if there is no operation performed.
Write a program that would minimize the number of possible operations to transform any string x into a string y.
Input
The input consists of the strings x and y prefixed by their respective lengths, which are within 1000.
Output
An integer representing the minimum number of possible operations to transform any string x into a string y.
Sample Input
10 AGTCTGACGC
11 AGTAAGTAGGC
Sample Output
4 关于最短编辑距离可以看一下我的蓝桥杯子栏中的文章,那里有些分析
这题我只是用来测试蓝桥杯里面的“DNA比对”这题,顺便水过的 - -
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; const int N = ;
int main()
{
int n1, n2;
char a[N], b[N];
while(cin >> n1 >> a >> n2 >> b)
{
int d1[N + ] = {}, d2[N + ] = {};
for(int i = ; i <= N; i++)
{
d1[i] = i;
} for(int i = ; i <= n1; i++) //a(0, i)
{
d2[] = i;
for(int j = ; j <= n2; j++) //b(0, j)
{
d2[j] = min(d2[j-] + , d1[j] + );
if(a[i - ] == b[j - ])
{
d2[j] = min(d2[j], d1[j-]);
}
else
{
d2[j] = min(d2[j], d1[j-] + );
}
}
for(int k = ; k <= N; k++)
{
d1[k] = d2[k];
}
}
cout << d2[n2] << endl;
}
return ;
}
POJ_3356——最短编辑距离,动态规划的更多相关文章
- (5千字)由浅入深讲解动态规划(JS版)-钢条切割,最大公共子序列,最短编辑距离
斐波拉契数列 首先我们来看看斐波拉契数列,这是一个大家都很熟悉的数列: // f = [1, 1, 2, 3, 5, 8] f(1) = 1; f(2) = 1; f(n) = f(n-1) + f( ...
- [LeetCode] 72. 编辑距离 ☆☆☆☆☆(动态规划)
https://leetcode-cn.com/problems/edit-distance/solution/bian-ji-ju-chi-mian-shi-ti-xiang-jie-by-labu ...
- TZOJ 1072: 编辑距离(动态规划)
1072: 编辑距离 时间限制(普通/Java):1000MS/10000MS 内存限制:65536KByte 总提交: 917 測试通过:275 描写叙述 如果字符串的 ...
- acwing 902. 最短编辑距离
地址 https://www.acwing.com/problem/content/904/ 给定两个字符串A和B,现在要将A经过若干操作变为B,可进行的操作有: 删除–将字符串A中的某个字符删除. ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- POJ 3356(最短编辑距离问题)
POJ - 3356 AGTC Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Desc ...
- 72. Edit Distance(编辑距离 动态规划)
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 题解【AcWing902】最短编辑距离
题面 经典的最长公共子序列模型. 我们设 \(dp_{i,j}\) 表示 \(a_{1...i}\) 与 \(b_{1...j}\) 匹配上所需的最少操作数. 考虑删除操作,我们将 \(a_i\) 删 ...
- 行编辑距离Edit Distance——动态规划
题目描写叙述: 给定一个源串和目标串.可以对源串进行例如以下操作: 1. 在给定位置上插入一个字符 2. 替换随意字符 3. 删除随意字符 写一个程序.返回最小操作数,使得对源串进行这些操作后等 ...
随机推荐
- 线程、委托、lambda运算符的简单示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 0_Linux_虚拟机安装
虚拟机的安装0Snapshot和clone 系统分区(由硬盘的性能所限制的) 1分区类型: 主分区:最多有4个 扩展分区:最多只能有1个:主分区加扩展分区最多有4个:不能写入数据,只能包含逻辑分区,不 ...
- 用CSS3实现带小三角形的div框(不用图片)
现在看到了很多带小三角形的方框,如微信.Mac版的QQ.QQ空间的时间轴等等,在聊天或者是发表的状态的内容外面都有一个带小三角形的矩形框包围着,感觉看着很不错,于是决定亲自动手写一个,我上次用的是偏移 ...
- vedeo与audio标签的使用
浏览器原生支持音视频无疑是一件大事——尤其对移动设备而言.不依赖Flash,意味着更加省电.安全和快速的播放体验,而且只需要引入一个标签,就能播放自如. <video src="dao ...
- asp.net 从客户端中检测到有潜在危险的 Request.Form 值错误解
从客户端(ftbContent="<P><A href="http://l...")中检测到有潜在危险的 Request.Form 值. 说明: 请求验 ...
- 利用iframe实现提交表单是页面部分刷新
直接上代码: <%@ page language="java" import="java.util.*" pageEncoding="utf-8 ...
- sqlite3 小结
sqlite安装 DDL(数据定义语言):create.alter.drop DML(数据操作语言):insert.update.delete DQL(数据查询语言):select sqlite3 命 ...
- 『重构--改善既有代码的设计』读书笔记----Split Temporary Variable
继续开始我们重构手法的系列,今天介绍的是Split Temporary Variable---分解临时变量. 在我们平常写的程序中肯定有某些临时变量被赋予了超过一个的责任.如果他们不是那种收集结果(t ...
- (转)QT常用快捷键
F1 查看帮助F2 跳转到函数定义(和Ctrl+鼠标左键一样的效果)Shift+F2 声明和定义之间切换F4 头文件和源文件之间切换Ctrl+1 ...
- 1 Two Sum(找和为target的两个数字下标Medium)
题目意思:给一个数组,找到和为target的两个元素的序号,并且只有一组这样的元素 思路:map<int,int>(nums[i],i+1),然后从后往前循环,用count找,比较i+1 ...