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. WebView js 调用Java本地方法

    webView = (WebView) this.findViewById(R.id.webview); WebSettings webSettings = webView.getSettings() ...

  2. C++使用模版技术将任意类型的数据转为某个类型的数据

    将任意类型(int, float, 自定义的数据类型等等)的数据转换的某个类型C中储存,可以通过 将类型C的构造函数写成模版函数的形式,在C中将可以接收任意类型数据.如: class C{ templ ...

  3. VK Cup 2015 - Round 1 -E. Rooks and Rectangles 线段树最值+扫描线

    题意: n * m的棋盘, k个位置有"rook"(车),q次询问,问是否询问的方块内是否每一行都有一个车或者每一列都有一个车? 满足一个即可 先考虑第一种情况, 第二种类似,sw ...

  4. Java环境变量的配置及意义

    配置环境:Windows XP 首先是配置参数,这个网站上很多.很多网站上说要配置三个参数,即Path,classpath,JAVA_HOME三个.   简要的介绍一下这三个环境变量. Path使得系 ...

  5. tomcat环境搭建

    前言 学习任何一门技术的都需要环境的搭建,Hello world成为学习的开端.今天我就为大家带来tomcat的环境搭建过程. 配置环境变量 右击我的电脑->属性->高级系统设置-> ...

  6. iOS中navigationItem的titleView如何居中

    开发过程中,发现titleview很难居中,通过各种尝试终于找到了解决方法. 首先清楚你个概念: leftBarButtonItem,导航条中左侧button. rightBarButtonItem, ...

  7. Servlet与JSP的关系

    Servlet与JSP的异同点: 相似点: 都可以生成动态网页 不同点: JSP擅长网页制作,生成页面直观,但不易追踪与排错 Servlet是纯Java,擅长处理流程与业务逻辑,缺点是页面不直观

  8. Git和Github的应用与命令方法总结

    title: Git和Github的应用与命令方法总结 date: 2016-07-11 14:03:09 tags: git/github [本文摘抄自微信公众平台:AndroidDeveloper ...

  9. SpringMVC11文件上传

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  10. aspx页面中获取当前浏览器url

    /假设当前浏览器地址为:http://www.360.net.cn/Group/Index.aspx?id=123 这其中如下介绍: ①."http://"是协议名 ②." ...