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


    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[1005][1005];
    char a[1005],b[1005];
    int m,n;
    int main()
    {
         while(~scanf("%d %s",&m,a+1))
         {
             scanf("%d %s",&n,b+1);
             int maxl=max(m,n);
             for(int i=0;i<=maxl;i++)
             {
                 dp[i][0]=i;
                 dp[0][i]=i;
             }
             for(int i=1;i<=m;i++)
            {
                for(int j=1;j<=n;j++)
             {
                 if(a[i]==b[j])
                    dp[i][j]=dp[i-1][j-1];
                 else
                    dp[i][j]=min(min(dp[i-1][j]+1,dp[i][j-1]+1),dp[i-1][j-1]+1);
             }
            }
             cout<<dp[m][n]<<endl;
         }
    }
  • 相关阅读:
    USACO2008 Cow Cars /// oj23323
    USACO2008 Roads Around The Farm /// queue oj23321
    USACO2007 捕牛记 /// queue+桶 oj1503
    哈理工多校算法赛一
    USACO2012 Haybale stacking /// 区间表示法 oj21556
    USACO2012 Broken necklace /// DP oj10103
    USACO2004 cube stacking /// 带权并查集 oj1302
    ACM-ICPC 2018 南京赛区网络预赛 J sum (找一个数拆成两个无平方因子的组合数)
    分层图 (可以选择K条路的权为0,求最短路)
    POJ 3537 Crosses and Crosses(sg博弈)
  • 原文地址:https://www.cnblogs.com/hfc-xx/p/4726143.html
Copyright © 2020-2023  润新知