POJ 3356(最短编辑距离问题)
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:
Certainly, we would like to minimize the number of all possible operations.
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 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 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(最短编辑距离问题)的更多相关文章
- POJ 3356 AGTC(最小编辑距离)
POJ 3356 AGTC(最小编辑距离) http://poj.org/problem?id=3356 题意: 给出两个字符串x 与 y,当中x的长度为n,y的长度为m,而且m>=n.然后y能 ...
- POJ_3356——最短编辑距离,动态规划
Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...
- poj 3356
Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...
- (5千字)由浅入深讲解动态规划(JS版)-钢条切割,最大公共子序列,最短编辑距离
斐波拉契数列 首先我们来看看斐波拉契数列,这是一个大家都很熟悉的数列: // f = [1, 1, 2, 3, 5, 8] f(1) = 1; f(2) = 1; f(n) = f(n-1) + f( ...
- 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(DP求字符串编辑距离)
给出两个长度小于1000的字符串,有三种操作,插入一个字符,删除一个字符,替换一个字符. 问A变成B所需的最少操作数(即编辑距离) 考虑DP,可以用反证法证明依次从头到尾对A,B进行匹配是不会影响答案 ...
- poj 3356 AGTC(线性dp)
题目链接:http://poj.org/problem?id=3356 思路分析:题目为经典的编辑距离问题,其实质为动态规划问题: 编辑距离问题定义:给定一个字符串source,可以对其进行复制,替换 ...
- POJ 3356.AGTC
问题简述: 输入两个序列x和y,分别执行下列三个步骤,将序列x转化为y (1)插入:(2)删除:(3)替换: 要求输出最小操作数. 原题链接:http://poj.org/problem?id=335 ...
- POJ 3356 水LCS
题目链接: http://poj.org/problem?id=3356 AGTC Time Limit: 1000MS Memory Limit: 65536K Total Submission ...
随机推荐
- 解决python “No module named pip”
python 升级后导致不能使用原来的pip命令 windows平台 cmd中敲命令:python -m ensurepip 得到pip的setuptools 然后就可以用:easy_install ...
- 融云的Java端实现
产品中要使用融云实现即时通讯,下面是实现步骤: 1 .注册appkey secret,这个是第一步: 2 .下载融云Java客户端 https://github.com/rongcloud/serve ...
- PNG文件格式具体解释
PNG文件结构分析(上:了解PNG文件存储格式) 前言 我们都知道,在进行J2ME的手机应用程序开发的时候,在图片的使用上,我们能够使用PNG格式的图片(甚至于在有的手机上,我们仅仅能够使用PNG ...
- Android ExpandableListView实例Demo
前几篇文章介绍了Listview.但在实际开发中也常常会用到多层的Listview来展示数据,比方qq中的好友展示,所以这张来了解一下ExpandableListview.基本思想与Listview大 ...
- [Javascript] Grouping and Nesting Console Log Output
Organize your log output by grouping your logs into collapsable hierarchies using console.group(). ; ...
- ScriptManager的使用方法
脚本管理控件(ScriptManger)是ASP.NET AJAX中很重要的控件,通过使用ScriptManger可以进行整个页面的局部更新的管理.ScriptManger用来处理页面上局部更新,同一 ...
- 《Android开发艺术探索》读书笔记 (7) 第7章 Android动画深入分析
本节和<Android群英传>中的第七章Android动画机制与使用技巧有关系,建议先阅读该章的总结 第7章 Android动画深入分析 7.1 View动画 (1)android动画分为 ...
- retrofit2 okhttp3 RxJava butterknife 示例
eclipse的jar包配置 eclipse中貌似用不了butterknife buildToolsVersion "23.0.2" defaultConfig { applica ...
- Web中常用字体介绍(转)
1.在Web编码中,CSS默认应用的Web字体是有限的,虽然在新版本的CSS3,我们可以通过新增的@font-face属性来引入特殊的浏览器加载字体. 浏览器中展示网页文字内容时,文字字体都会按照设计 ...
- MVC使用Exception过滤器自定义处理Action的的异常
1.继承FilterAttribute ,IExceptionFilter自定义处理 /// <summary> /// 登录错误自定义处理 /// </summary> pu ...