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, dp[i][j-1] + 1)

假设修改的是x串,那么右侧三项分别对应改变(如果已经相同则不必),删除,插入。

然后发现实际上是对称的,修改的是y串,三项对应改变,插入,删除。

我感觉如果允许同时修改两串,其实方程也一样,结果也一样,因为删除x串对应插入y串,插入y串对应删除x串。

然后这题坑爹在于多组case,看了discuss才知道。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5
 6 int lx, ly, oo;
 7 char x[1100], y[1100];
 8 int dp[1100][1100];
 9 bool same(int i, int j)
10 {
11     if (x[i] == y[j]) return true;
12     else return false;
13 }
14 int main()
15 {
16     while(scanf("%d %s %d %s", &lx, x, &ly, y) != EOF)
17     {
18         memset(dp, 127, sizeof(dp));
19         oo = dp[0][0];
20         dp[0][0] = 0;
21         for (int i = 0; i <= lx; i++)
22             for (int j = 0; j <= ly; j++){
23                 if (i > 0 && j > 0 && dp[i-1][j-1] != oo)
24                     dp[i][j] = min(dp[i][j], dp[i-1][j-1] + 1 - same(i-1, j-1)); //Change
25                 if (i > 0 && dp[i-1][j] != oo)
26                     dp[i][j] = min(dp[i][j], dp[i-1][j] + 1);             //Deletion of x or Insertion of y
27                 if (j > 0 && dp[i][j-1] != oo)
28                     dp[i][j] = min(dp[i][j], dp[i][j-1] + 1);             //Insertion of x or Deletion of y
29             //    printf("%d %d %d\n", i, j, dp[i][j]);
30             }
31         printf("%d\n", dp[lx][ly]);
32     }
33     return 0;
34 }

POJ 3356 AGTC 最短编辑距离 DP,布布扣,bubuko.com

时间: 2024-08-02 02:48:07

POJ 3356 AGTC 最短编辑距离 DP的相关文章

POJ 3356 AGTC (编辑距离 DP)

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

POJ 3356 AGTC(最小编辑距离)

http://poj.org/problem?id=3356 题意: 给出两个字符串x 与 y,当中x的长度为n,y的长度为m,而且m>=n.然后y能够经过删除一个字母,加入一个字母,转换一个字母,三种操作得到x.问最少能够经过多少次操作 分析: 我们令dp[i][j]==x表示源串的前i个字符变成目串的前j个字符须要x步操作. 初始化: dp[0][i]==i且 dp[i][0]=i. 上述前者表示加入源串i个字符, 后者表示删除源串i个字符. 状态转移: dp[i][j]假设我们仅仅在源串的

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

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 

POJ 3356 AGTC(最长公共子序列)

AGTC 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 missin

POJ 3356.AGTC

问题简述: 输入两个序列x和y,分别执行下列三个步骤,将序列x转化为y (1)插入:(2)删除:(3)替换: 要求输出最小操作数. 原题链接:http://poj.org/problem?id=3356 解题思路: 明显的动态规划题,输入两个字符串 a[0...m-1] , b[0...n] 使用二维数组 dp[i,j] 记录 a[0...i] 和 b[0...j] 对应的最小操作数 显然有以下递归方程: dp[i,0] = i dp[0,j] = j dp[i,j] = dp[i-1,j-1]

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

给出两个长度小于1000的字符串,有三种操作,插入一个字符,删除一个字符,替换一个字符. 问A变成B所需的最少操作数(即编辑距离) 考虑DP,可以用反证法证明依次从头到尾对A,B进行匹配是不会影响答案的 令dp[i][j]表示A[i]~[lenA]变成B[j]~[lenB]的最优解. 如果把B[j]插入到A[i]前,dp[i][j]=dp[i][j+1]+1 如果删除A[i],dp[i][j]=dp[i+1][j]+1. 如果A[i]==B[j], dp[i][j]=dp[i+1][j+1].

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

poj3356 字符串的最小编辑距离 dp

poj3356 字符串的最小编辑距离  dp AGTC Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10895   Accepted: 4188 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 bel

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: Deletion: a letter in x is missing in y at a corresponding position. Insertion: a letter in y is missing in