"Codevs 2598" editing distance problem

Source: Internet
Author: User
Tags time limit

2598 Editing distance problems
Time limit: 1 s
Space limit: 128000 KB
Title Level: Diamonds Diamond
Exercises
Title Description Description
Set A and B are 2 strings. To convert string A to string B with a minimum of character manipulation. The character operations described here include:

(1) Delete a character;

(2) inserting a character;

(3) Change one character to another character.

The minimum character operand used to transform string A into string B is called the editing distance of the string A through B, which is recorded as D (A, A, a). Try to write the program, on the given 2 strings A and B, calculate their editing distance d (A, a, a, a,).

Enter a description input Description
The input file edit.in has two lines, the first line is string A, and the second line is string B.

Outputs description Output Description
The output file edit.out only one line, which is the editing distance d (A, b).

Sample input to sample
Fxpimu

Xwrs

Sample output Sample Outputs
5

Data Size & Hint
The length of the 40% data string A and B is not more than 100;

The length of the 100% data string A, B is no more than 4000.

DP[I][J] The editing distance of a string before I and B string before J characters

Dp[i][j] = i; (j = = 0)
Dp[i][j] = j; (i = = 0)
Dp[i][j] = min (Dp[i-1][j] + 1,dp[i][j-1] + 1,dp[i-1][j-1] + 1); (A[i]! = B[i])
Dp[i][j] = dp[i-1][j-1] + 1; ( A[i] = = B[i])

 #include <iostream> #include <cstdio> #include <cstring> #include <
Cmath> #include <algorithm> using namespace std;
const int MAXN = 4002;
String A, B;
Char SA[MAXN],SB[MAXN];
int DP[MAXN][MAXN];
    int main () {memset (dp,0x3f,sizeof (DP));
    Cin >> A;
    CIN >> B;
    int n = a.length ();
    int m = B.length ();
    for (int i = 1; I <= n; i + +) sa[i] = a[i-1];
    for (int i = 1; I <= m; i + +) sb[i] = b[i-1];
    Dp[0][0] = 0;
    for (int i = 1; I <= n; i + +) dp[i][0] = i;
    for (int i = 1; I <= m; i + +) dp[0][i] = i;
                for (int i = 1; I <= n; i + +) {for (int j = 1; j <= M; j + +) {if (Sa[i]! = Sb[j])
            Dp[i][j] = min (dp[i-1][j-1] + 1,min (dp[i][j-1] + 1,dp[i-1][j] + 1));
        else dp[i][j] = dp[i-1][j-1];
    }} printf ("%d\n", Dp[n][m]);
return 0; }

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: info-contact@alibabacloud.com 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.