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. 删除随意字符 写一个程序.返回最小操作数,使得对源串进行这些操作后等 ...
随机推荐
- Swift: The Basics
Swift是类型安全的语言: Swift introduces optional types, which handle the absence of a value. Optional say ei ...
- [转] 学习HTML/JavaScript/PHP 三者的关系以及各自的作用
1.What is HTML? When you write a normal document using a word processor like Microsoft Word/Office, ...
- apk代码的破解
方法一:dexdump方法(效果很不好,推荐指数*) 1.搜索到dexdump.exe所在目录: 2.将apk包中的**.dex文件存放到上面目录: 3.命令行中进入上面目录,执行:dexdump ...
- MyBatis 学习总结(一)
1.原生JDBC(Java database connectity)操作数据库(以MySQL数据为例)步骤 1.1 加载驱动 Class.forName("com.mysql.jdbc.Dr ...
- centos7.2下安装mysql5.7,使用rpm包安装
0.环境 本文操作系统: CentOS 7.2.1511 x86_64 MySQL 版本: 5.7.16 1.卸载系统自带的 mariadb-lib[root@centos-linux ~]# rpm ...
- 剪切板 复制文本 ClipboardManager
代码 public class MainActivity extends ListActivity { private EditText tv_info; private Clipbo ...
- Activity 【生命周期】
不同情况下的回调 我们打开应用时先后调用了onCreate()->onStart()->onResume 当我们按BACK键时,我们这个应用程序将结束,这时候我们将先后调用onPause( ...
- js的异步的问题的再次理解
*js是实实在在的单线程语言,在一小个时刻,在(js的执行对列)只有一个执行,一个没有完,另一个必须等待,什么都不做,只有抖着腿的等; *本来语言本身是同步的,之所以是异步执行,是因为在浏览器环境中, ...
- Java POI 导出excel表
1.首先下载poi-3.6-20091214.jar,下载地址如下: http://download.csdn.net/detail/evangel_z/3895051 2.Student.java ...
- ecshop首页调用指定商品分类下的商品品牌列表
转之--http://www.16css.com/ecshop/735.html 通过二次开发可以实现ECSHOP首页调用指定分类下的品牌列表. 第一步: 打开根目录下的index.php 在最后面 ...