POJ 3356 AGTC (minimum editing distance), pojagtc

Source: Internet
Author: User

POJ 3356 AGTC (minimum editing distance), pojagtc

POJ 3356 AGTC (minimum editing distance)

Http://poj.org/problem? Id = 3356

Question:

Two strings x and y are given. The length of x is n, the length of y is m, and m> = n. then, y can delete a letter, add a letter, and convert a letter to x. how many operations can be performed at least?

Analysis:

We set dp [I] [j] = x to indicate that the first I characters of the source string must be x-step.

Initialization: Dp [0] [I] = I and dp [I] [0] = I.

The former indicates that the source string is added with I characters, and the latter indicates that the source string is deleted with I characters.

Status Transfer: Dp [I] [j] If weOnly at the end of the source stringPerform the following three operations.

1. dp [I-1] [J-1] + (x [I] = y [j]? 0: 1). If the source string and the object string last character is the same, natural dp [I-1] [J-1] is to ask. If they last character is different, then you canReplaceThe last character of the source string is the character of the object string.

2. dp [I-1] [j] + 1. We canDeleteThe last character of the source string (step 1 delete operation), and then the source string into the object string (dp [I-1] [j] Step operation ).

3. dp [I] [J-1] + 1. We can convert the source string to the string of the first J-1 character (Dp [I] [J-1] Step operations), And then at the end of the source stringAddA y [j] becomes the object string (Step 1).

Final Demand: Minimum values of the preceding three formulas.

AC code:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=1000+5;int n,m;int dp[maxn][maxn];char s1[maxn],s2[maxn];int main(){    while(scanf("%d%s",&n,s1)==2)    {        scanf("%d%s",&m,s2);        for(int i=0;i<=m;i++)            dp[0][i]=i;        for(int i=0;i<=n;i++)            dp[i][0]=i;        for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)        {            dp[i][j]=dp[i-1][j-1]+(s1[i-1]==s2[j-1]?0:1);            dp[i][j]=min(dp[i][j], min(dp[i-1][j]+1,dp[i][j-1]+1));        }        printf("%d\n",dp[n][m]);    }    return 0;}


Min editing distance pascal

Please describe the problem
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: [email protected] and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.