POJ - 3356

Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

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 C

Deletion: * 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

题解:给你两行字符串,第一行为A长度m,第二行为B,长度n,dp[i][j]表示编辑A前i字母和编辑B的前j个字母所需要的操作次数。这里我们要求最少的操作次数。

我们可以执行的操作有删除,插入,还有替换。

解题思路:

长当某一个字符串为空时,那么可以得到dp[0][i] = i和dp[i][0]=i,因为某一字符串为空的,要得到另一个i长度字符串,必须经过i次插入操作。

如果A比B:

如果没有空字符串,有3中操作选择,
1.替换 ,将a[0]和b[0]判断,如果相等,即返回上一个字符的,操作次数加1,即dp[i-1][j-1]+1.
2.删除,所以删除A一个字符,
也就是必须有一次操作,删除A[ ]后,返回A的前一个字符,即dp[i-1][j]+1
3.插入,在B添加一个字符,A不变,如果执行完相等,就要找B的前一个去匹配,所以要减1,和A去匹配,执行了一次,操作次数要加1,即dp[i][j-1] + 1

如果B比A长,则删除和插入交换。

每次选出这三种执行操作数最少的就可以了。

下面的代码

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[][];
char a[],b[];
int m,n;
int main()
{
while(~scanf("%d %s",&m,a+))
{
scanf("%d %s",&n,b+);
int maxl=max(m,n);
for(int i=;i<=maxl;i++)
{
dp[i][]=i;
dp[][i]=i;
}
for(int i=;i<=m;i++)
{
for(int j=;j<=n;j++)
{
if(a[i]==b[j])
dp[i][j]=dp[i-][j-];
else
dp[i][j]=min(min(dp[i-][j]+,dp[i][j-]+),dp[i-][j-]+);
}
}
cout<<dp[m][n]<<endl;
}
}

POJ 3356(最短编辑距离问题)的更多相关文章

  1. POJ 3356 AGTC(最小编辑距离)

    POJ 3356 AGTC(最小编辑距离) http://poj.org/problem?id=3356 题意: 给出两个字符串x 与 y,当中x的长度为n,y的长度为m,而且m>=n.然后y能 ...

  2. POJ_3356——最短编辑距离,动态规划

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  3. poj 3356

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  4. (5千字)由浅入深讲解动态规划(JS版)-钢条切割,最大公共子序列,最短编辑距离

    斐波拉契数列 首先我们来看看斐波拉契数列,这是一个大家都很熟悉的数列: // f = [1, 1, 2, 3, 5, 8] f(1) = 1; f(2) = 1; f(n) = f(n-1) + f( ...

  5. POJ 3356 AGTC(DP-最小编辑距离)

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  6. POJ 3356 AGTC(DP求字符串编辑距离)

    给出两个长度小于1000的字符串,有三种操作,插入一个字符,删除一个字符,替换一个字符. 问A变成B所需的最少操作数(即编辑距离) 考虑DP,可以用反证法证明依次从头到尾对A,B进行匹配是不会影响答案 ...

  7. poj 3356 AGTC(线性dp)

    题目链接:http://poj.org/problem?id=3356 思路分析:题目为经典的编辑距离问题,其实质为动态规划问题: 编辑距离问题定义:给定一个字符串source,可以对其进行复制,替换 ...

  8. POJ 3356.AGTC

    问题简述: 输入两个序列x和y,分别执行下列三个步骤,将序列x转化为y (1)插入:(2)删除:(3)替换: 要求输出最小操作数. 原题链接:http://poj.org/problem?id=335 ...

  9. POJ 3356 水LCS

    题目链接: http://poj.org/problem?id=3356 AGTC Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...

随机推荐

  1. windows Compiler toolchain env

    1,gygwin

  2. linux运维社区站点收集

    1, 新世纪linux社区 offical locator: http://www.21ops.com/industry-news/24370.html 2,

  3. redis合库

    玩家数据全部保存在redis,对合服来绝对是个坑.因为一直都是做开发,合库这事还是第一次操作. 首先,合服要做哪些事情,当然不同的游戏肯定不一样.合服的目的是为了增加同个服务器上活跃玩家的数量.合服有 ...

  4. Girls' research - HDU 3294 (Manacher处理回文串)

    题目大意:给以一个字符串,求出来这个字符串的最长回文串,不过这个字符串不是原串,而是转换过的,转换的原则就是先给一个字符 例如 'b' 意思就是字符把字符b转换成字符 a,那么c->b, d-& ...

  5. Heavy Transportation

    题目大意: 雨果的沉重运输是快乐的,当浮空运输出现故障时候他可以扩展业务, 但他需要一个聪明的人告诉他是否真的是一种把他的客户构建了巨型钢起重机的地方需要的所有街道都可以承受重量(这句是直接有道翻译的 ...

  6. Scrapy:Python的爬虫框架

    网络爬虫,是在网上进行数据抓取的程序,使用它能够抓取特定网页的HTML数据.虽然我们利用一些库开发一个爬虫程序,但是使用框架可以大大提高效率,缩短开发时间.Scrapy是一个使用Python编写的,轻 ...

  7. sqlserver使用户只能在某个架构下建立表和存储过程

    1.首先,建立一个用户之后,默认的架构是dbo,默认的角色是public.这种情况下,这个用户将看不到dbo以及其他架构下的对象.除非单独进行授权.   2.新建一个架构test,然后使得这个架构的所 ...

  8. serialVersionUID的作用以及设置方法(转)

    声明:本篇文章是转载的 http://blog.csdn.net/kakaxi_77/article/details/8129070 http://snowlotus.iteye.com/blog/2 ...

  9. Git git rebase 使用

      原文:http://gitbook.liuhui998.com/4_2.html 一.基本 git rebase用于把一个分支的修改合并到当前分支. 假设你现在基于远程分支"origin ...

  10. [转] Javascript中数组与字典(即object)的使用

    简述: 简单记录一下数据结构字典和数组, 其实在Javascript这种弱类型的脚本语言中,数组同时也就是字典,下面主要就是字典数组的简易使用 代码: 1. 数组中添加map <!DOCTYPE ...