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
+ 1);注意初始化dp[i][0] = dp[0][i] = i;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1010;
char strx[maxn], stry[maxn];
int lenx, leny, dp[maxn][maxn];
int main()
{ while( scanf("%d %s", &lenx, strx + 1) != EOF)
{
scanf("%d %s", &leny, stry + 1);
int maxv = max(lenx, leny);
dp[0][0] = 0;
for(int i = 1; i <= maxv; i++)
dp[0][i] = dp[i][0] = i;
for(int i = 1; i <= lenx; i++)
{
for(int j = 1; j <= leny; j++)
{
dp[i][j] = min(dp[i-1][j] + 1, dp[i][j-1] + 1);
if(strx[i] == stry[j])
dp[i][j] = min(dp[i][j], dp[i-1][j-1]);
else
dp[i][j] = min(dp[i][j], dp[i-1][j-1] + 1);
}
}
printf("%d\n", dp[lenx][leny]);
} return 0;
}
poj 3356的更多相关文章
- POJ 3356(最短编辑距离问题)
POJ - 3356 AGTC Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Desc ...
- POJ 3356 AGTC(最小编辑距离)
POJ 3356 AGTC(最小编辑距离) http://poj.org/problem?id=3356 题意: 给出两个字符串x 与 y,当中x的长度为n,y的长度为m,而且m>=n.然后y能 ...
- POJ 3356.AGTC
问题简述: 输入两个序列x和y,分别执行下列三个步骤,将序列x转化为y (1)插入:(2)删除:(3)替换: 要求输出最小操作数. 原题链接:http://poj.org/problem?id=335 ...
- poj 3356 AGTC(线性dp)
题目链接:http://poj.org/problem?id=3356 思路分析:题目为经典的编辑距离问题,其实质为动态规划问题: 编辑距离问题定义:给定一个字符串source,可以对其进行复制,替换 ...
- POJ 3356 水LCS
题目链接: http://poj.org/problem?id=3356 AGTC Time Limit: 1000MS Memory Limit: 65536K Total Submission ...
- POJ 3356 AGTC(DP-最小编辑距离)
Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...
- POJ 3356 AGTC(最长公共子)
AGTC Description Let x and y be two strings over some finite alphabet A. We would like to transform ...
- POJ 3356 AGTC(DP求字符串编辑距离)
给出两个长度小于1000的字符串,有三种操作,插入一个字符,删除一个字符,替换一个字符. 问A变成B所需的最少操作数(即编辑距离) 考虑DP,可以用反证法证明依次从头到尾对A,B进行匹配是不会影响答案 ...
- Poj 3356 ACGT(LCS 或 带备忘的递归)
题意:把一个字符串通过增.删.改三种操作变成另外一个字符串,求最少的操作数. 分析: 可以用LCS求出最大公共子序列,再把两个串中更长的那一串中不是公共子序列的部分删除. 分析可知两个字符串的距离肯定 ...
随机推荐
- vim分屏功能
转载,来自http://coolshell.cn/articles/1679.html 本篇文章主要教你如何使用 Vim 分屏功能. 分屏启动Vim 使用大写的O参数来垂直分屏. vim -On fi ...
- Android进程回收机制LMK(Low Memory Killer)【转】
本文转载自:http://www.cnblogs.com/wytiger/p/5744752.html 熟悉Android系统的童鞋都知道,系统出于体验和性能上的考虑,app在退到后台时系统并不会真正 ...
- JavaScript:Browser 对象
ylbtech-JavaScript:Browser 对象 1. Window 对象返回顶部 1. Window 对象 Window 对象 Window 对象表示浏览器中打开的窗口. 如果文档包含框 ...
- Node.js:事件循环
ylbtech-Node.js:事件循环 1.返回顶部 1. Node.js 事件循环 Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node.js 的每一个 ...
- python之路——二分查找算法
楔子 如果有这样一个列表,让你从这个列表中找到66的位置,你要怎么做? l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72 ...
- WPF Menu控件自定义Style
自定义WPF中Menu控件的样式
- Java基础10一面向对象
抽象 概念:当一个类中没有足够的信息描述一个现实生活中具体存在的事物,那么这个类就是抽象类. 抽象类一般是对概念领域中的描述. 语法: [访问修饰符] abstract class 类名{ } 如: ...
- VTK+MFC 系列教程 非常强大
虽然QT才是王道!MFC的懂一些也是好的. 原文链接:http://blog.csdn.net/www_doling_net/article/details/8939115 之前介绍了基于VTK的单文 ...
- Validate US Telephone Numbers FreeCodeCamp
function telephoneCheck(str) { // 祝你好运 //var re = /^1? ?(\(\d{3}\)|\d{3})[ |-]?\d{3}[ |-]?\d{4}$/; / ...
- 洛谷P2894 [USACO08FEB]酒店Hotel_区间更新_区间查询
Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...