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. 删除随意字符 写一个程序.返回最小操作数,使得对源串进行这些操作后等 ...
随机推荐
- Protobuf的自动反射消息类型的方法
1. 每个消息头部中带上type name,作为消息的类型标识 2. 通过type name可以找到描述符Descriptor*, FindMessageTypeByName 3. 通过描述符Desc ...
- linux 下各errno的意义
strerror(errno):获取errno对应的错误 #include <string.h> /* for strerror */ #include <errno.h> # ...
- iOS仿喜马拉雅FM做的毕业设计及总结(含新手福利源码)
其实仿喜马拉雅FM很早就开始了,从我刚接触iOS开始,就开始仿做了一部分,眼尖的人都从我的github找到了那个项目.随着找到实习iOS工作,仿写就落下了,但唯一的收获就是给过去打了一个响亮的耳光,因 ...
- java定时器,Spring定时器和Quartz定时器
一.java定时器的应用 其实java很早就有解决定时器任务的方法了,java提供了了类java.util.TimerTask类基于线程的方式来实现定时任务的操作,然后再提供java.util.Tim ...
- C#中的IO流操作(FileStream)
StreamReader和StreamWriter适用于对文本文件的操作,因为它是以字符为单位进行的操作 不用担心编码问题 using (Stream s = new FileStream(@&quo ...
- 转载:C# HashSet 用法
原文地址:http://www.cnblogs.com/xiaopin/archive/2011/01/08/1930540.html 感谢博主分享! NET 3.5在System.Collect ...
- 在ASP.NET MVC5 及 Visual Studio 2013 中为Identity账户系统配置数据库链接及Code-First数据库迁移
在ASP.NET MVC5 及 Visual Studio 2013 中为Identity账户系统配置数据库链接及Code-First数据库迁移 最近发布的ASP.NET MVC 5 及Visual ...
- jQueryUI 日期控件
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...
- bootstrap栅格布局
<!DOCTYPE html> <html lang="en"> <head> <!-- //简介:boststrap内置了一套响应式,移 ...
- (转)ThinkPHP系统常量
__ROOT__ : 网站根目录地址 __APP__ : 当前项目(入口文件)地址 __URL__ : 当前模块地址 __ACTION__ : 当前操作地址 __SELF__ : 当前 URL 地址 ...