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
nm.

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
题意:
求由字符串s1,通过下列三种操作:
1.插入一个字符
2.删除一个字符
3.改变一个字符
变换的字符s2所须要 的最小操作次数。
思路:这是一个求编辑最短距离问题。利用动态规划,列出状态方程,设dp[i][j]表示字符串x[1...i]和字符串y[1...j]的最短编辑距离当x[i] == y[j]时,i和j不须要编辑,要么删除,要么插入。要么替换dp[i][j] = min(dp[i-1][j-1], dp[i-1][j] + 1, dp[i][j - 1] + 1)当x[i] != y[i]时, i和j不须要编辑dp[i][j] = min(dp[i-1][j-1] + 1, dp[i-1][j] + 1, dp[i][j-1]
+ 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的更多相关文章

  1. POJ 3356(最短编辑距离问题)

    POJ - 3356 AGTC Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Desc ...

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

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

  3. POJ 3356.AGTC

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

  4. poj 3356 AGTC(线性dp)

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

  5. POJ 3356 水LCS

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

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

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

  7. POJ 3356 AGTC(最长公共子)

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

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

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

  9. Poj 3356 ACGT(LCS 或 带备忘的递归)

    题意:把一个字符串通过增.删.改三种操作变成另外一个字符串,求最少的操作数. 分析: 可以用LCS求出最大公共子序列,再把两个串中更长的那一串中不是公共子序列的部分删除. 分析可知两个字符串的距离肯定 ...

随机推荐

  1. 配置win2008防火墙&nbsp;允许被Ping的设置方法

    出于安全因素考虑,在 Windows Server 2008 上是不允许从外部对其执行 Ping 指令的 如果要配置允许被 Ping 通过以往的设置步骤会发现并不能从 Windows firewall ...

  2. java+appium+安卓模拟器实现app自动化Demo

    网上有比较多相关教程,自己写一遍,加深下印象. 环境搭建 据说,很多人都被繁琐的环境搭建给吓到了. 是的,确实,繁琐. node.js 网址 cmd输入node -v,出现下图说明成功. JDK 网址 ...

  3. redis实现计数--------Redis increment

    经理提出新的需求,需要知道每天微信推送了多少条模板消息,成功多少条,失败多少条,想到用Redis缓存,网上查了一些资料,Redis中有方法increment,测试代码如下 Controller imp ...

  4. C# 数组动态添加新元素的 方法

    经常在开发中  会对字符串 进行split 拆分操作.. 得到数组后再去做相应的事情! 但有时候,需求决定了 数组的长度 不是固定的, 而C# 数组 是不允许动态添加新的元素的.. 这事情让我也纠结了 ...

  5. Web启动,停止Windows服务

    When you grow stronger,the world become more dangerous.当你变得越强大,这个世界反而会变得越危险. ServiceModel.cs代码: publ ...

  6. Service、chkconfig命令

    转自:http://my.oschina.net/phptiger86/blog/137656

  7. BigDataMini导论

    Q: BigDataMini从大量数据中挖掘有用的信息,对AI有何意义? A: 随着智能硬件化,DataMini可以作为AI的一种数据筛选方法,简化AI的设计进程.

  8. 杭电 1021 Fibonacci Again

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1021 解题思路:根据之前发现斐波那契数列的规律,即为f(n)能被3整除当且仅当n能被4整除. 于是联想 ...

  9. win7不需要密码访问网络共享文件(转载)

    大家是不是觉得,共享一个文件夹后,每次输入密码都很麻烦呢?有没有不需要输入密码就可以访问共享文件的方法呢? 答案是肯定的,当然有,下面介绍下win7不需要密码访问网络共享文件 工具/原料 两台电脑在局 ...

  10. Win7系统无法记住网络访问的凭据怎么办?

    访问内网其他IP时已经打勾记住我的凭据了,但是注销或者重启Win7系统之后,又得重新输入用户名跟密码怎么办? 下面就为大家介绍一下如何让Win7系统记住网络访问的凭据. 以上问题还会引发,重启后无法正 ...