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. 删除随意字符 写一个程序.返回最小操作数,使得对源串进行这些操作后等 ...
随机推荐
- 图片预览(base64和blob:图片链接)和ajax上传、下载(带进度提示)
直接上代码 html和js <!DOCTYPE html> <html> <head> <meta name="viewport" con ...
- Block小结
Blocks是C语言的扩充功能.用一句话来表示Blocks的扩充功能:带有自动变量(局部变量)的匿名函数. block其实是一个代码块,但是它的神奇之处在于在内联(inline)执行的时候(这和C++ ...
- 常用 cmd 命令
msconfig-------系统配置实用程序 mspaint--------画图板 devmgmt.msc--- 设备管理器 diskmgmt.msc---磁盘管理实用程序 services.msc ...
- BaseBean构造
package cn.jsonlu.passguard.model; import cn.jsonlu.passguard.utils.MD5Util; import com.fasterxml.ja ...
- codevs 1139 观光公交
#include<cstdio> #include<cstdlib> #include<cstring> #define max(a,b) (a > b ? ...
- POJ 2411.Mondriaan's Dream 解题报告
题意: 给出n*m (1≤n.m≤11)的方格棋盘,用1*2的长方形骨牌不重叠地覆盖这个棋盘,求覆盖满的方案数. Solution: 位运算+状态压缩+dp ...
- Google jQuery URL
Query 在线地址:https://developers.google.com/speed/libraries/devguide?hl=zh-CN#jquery此地址里还包含了很多的JS框架.
- Java中的ExceptionInInitializerError异常及解决方法
当在静态初始化块中出现了异常的时候,JVM会抛出 java.lang.ExceptionInInitializerError异常.如果你了解Java中的静态变量,你会知道它们是在类加载的时候进行初始化 ...
- 初涉JavaScript模式 (9) : 函数 【常用方式】
回调模式 上一篇,对JavaScript函数进行了大体的介绍,这一篇对一些在工作中经常遇到的情况进行扩展. 在工作中,我们经常遇到很多需求,比如现在有一个需求: 一栋10层的大楼,当我们在坐电梯时,电 ...
- eval("表达式")
eval就是把字符串转成可执行代码eval("表达式");表达式被翻译成JavaScript代码执行比如eval("alert('test')");等于aler ...