poj4323 最短编辑距离

AGTC

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12240   Accepted: 4594

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

Source

Manila 2006

这道题是典型的DP问题最短编辑距离。

其状态转移方程为

  1. #include <stdio.h>
  2. #define MAXN 1024
  3. int dp[MAXN][MAXN];
  4. char str1[MAXN],str2[MAXN];
  5. int min3(int a,int b,int c){
  6. int min=a;
  7. if(min>b) min=b;
  8. if(min>c) min=c;
  9. return min;
  10. }
  11. int main()
  12. {
  13. int n,m;
  14. while(scanf("%d%s",&n,str1)!=EOF){
  15. scanf("%d%s",&m,str2);
  16. for(int i=0;i<=n;i++)
  17. dp[i][0]=i;
  18. for(int j=0;j<=m;j++)
  19. dp[0][j]=j;
  20. int count;
  21. for(int i=1;i<=n;i++){
  22. for(int j=1;j<=m;j++){
  23. str1[i-1]==str2[j-1]?count=0:count=1;
  24. dp[i][j]=min3(dp[i-1][j-1]+count,dp[i-1][j]+1,dp[i][j-1]+1);
  25. }
  26. }
  27. printf("%d\n",dp[n][m]);
  28. }
  29. return 0;
  30. }

http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244545
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244541
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244538
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244527
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244528
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244529
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244530

时间: 2024-08-04 18:31:52

poj4323 最短编辑距离的相关文章

最短编辑距离算法

一般情况下,电商在当客户输入一个不存在的商品时,会返回客户一个与客户输入最为接近的商品,并加以提示"您是不是在找XXX?".这其中用到了一种算法,叫做"最短编辑距离算法",能在一大堆已存在的字符串中找到与原字符串最为接近的那个字符串,称之为最短编辑距离. 这种算法是基于动态规划思想,下面是算法的思路描述: 描述: 设A和B是2个字符串.要用最少的字符操作将字符串A转换为字符串B.这里所说的字符操作包括: (1)删除一个字符; (2)插入一个字符: (3)将一个字符改

POJ 3356 AGTC 最短编辑距离 DP

http://poj.org/problem?id=3356 题意: 给两个长度不大于1000的串,修改其中一个串使得两串相同,问最少修改次数.修改有三种,插入一个字符,删除一个字符,改变一个字符. 分析: 直接给方程. dp[i][j]表示第一个串前i位和第二串前j位匹配的最小修改次数. dp[0][0] = 0, dp[length(x)][length(y)]为答案. dp[i][j] = min(dp[i-1][j-1] + x[i] != y[j], dp[i-1][j] + 1, d

算法题目: 动态规划 之 最短编辑距离

问题: 对于长度相同的2个字符串A和B,其距离定义为相应位置字符距离之和.2个非空格字符的距离是它们的ASCII码之差的绝对值:空格与空格的距离为0,空格与其他字符的距离为一个定值k.在一般情况下,字符串A和B的长度不一定相同.字符串A的扩展是在A中插入若干空格字符所产生的字符串.在字符串A和B的所有长度相同的扩展中,有一对距离最短的扩展,该距离称为字符串A和B的扩展距离.对于给定的字符串A和B,设计一个算法,计算其扩展距离. 测试数据: 输入:cmc      snmn        2   

最短编辑距离算法实现

一,算法介绍 在CS124课程的第一周提到 求解两个字符串相似度的算法---Minimum Edit Distance(最短编辑距离)算法.该算法在NLP(自然语言处理)中也会用到. 如何定义相似度呢?任给两个字符串X 和Y,使用以下三种操作将 字符串X 变到 字符串Y  :①插入(Insert)操作:②删除操作(delete):③替换操作(substitute) 比如 字符串X="intention" ,  字符串Y="execution".从字符串X 转换成 字

hdu4271 Find Black Hand 2012长春网络赛E题 最短编辑距离

hdu4271 Find Black Hand  2012长春网络赛E题  最短编辑距离 Find Black Hand Time Limit : 5000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 19   Accepted Submission(s) : 1 Problem Description I like playing game with my friend

最短编辑距离算法(字符串比较)

一.编辑距离 1.从字符串a变为字符串b所需要的元操作有3种: 增加一个字符 删除一个字符 变化一个字符 2.编辑距离:从字符串a变为b所需要的最少操作步骤. 二.最短编辑距离(动态规划) 首先定义一个函数--step(i, j),它表示第一个字符串的长度为i的子串到第二个字符串的长度为j的子串的编辑距离. 显然可以有如下动态规划公式: if i == 0 且 j == 0,step(i, j) = 0 if i == 0 且 j > 0,step(i, j) = j if i > 0 且j

最短编辑距离问题理解

最短编辑距离是指两个字符串,把其中一个字符串转为另一个字符串所需要花费的最小操作成本. 设dp[i][j]为Xi与Yj的最短编辑距离,则Xi与Yj处于最优解时的排列有三种情况 1.Xi最后一个元素xi位于Yj最后一个元素yj的左边 2.Xi最后一个元素xi位于Yj最后一个元素yj的右边 3.Xi最后一个元素xi与Yj最后一个元素yj重合 无论最终dp[i][j]的字符串如何对齐,只用取三种情况的最小值即可. 对于1,转化操作时,yj是必然要删掉的,故必然有1个单位的操作成本,因为dp[i][j]

最短编辑距离

最短编辑距离 1.第一行表示从ME到空字符所要删除的字符的所以情况 2.第一列表示从空字符到MY所需要插入字符的所有情况 3.斜箭头表示相同字符不需要替换,不相同字符所有替换次数的所有情况 function levenshteinDistance(a,b){ //生成表 const distanceMatix = Array(a.length + 1).fill(null).map(() => Array(b.length + 1).fill(null)) //第一行的修改次数 for(let

动态规划——最短编辑距离

编辑距离是指两个字串之间,从一个转成另一个所需要的最少编辑操作次数,许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符. 问题     给定两个字符串S1和S2,求S2和S1的编辑距离,即至少需要经过多少步编辑操作才可以将S1变成S2. 分析     定义“状态” edit[i][j],表示将S1的长度为i的前缀S1[1...i]到S2的长度为j的前缀S2[1....j]的编辑距离.则显然有: if (i == 0 && j == 0) edit[i][j] = 0;