sdutoj1225--Editing Distance (DP: string conversion)

Source: Internet
Author: User

Edit Distance

Nid=24#time "style=" padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; Color:rgb (83,113,197); Text-decoration:none; padding-top:0px ">time limit:1000ms Memory limit:65536k have questions? Dot here ^_^

Description of the topic if the basic operation of a string is only: Delete a character, insert a character, and change one of the characters to one character. Three operations.


We have carried out a random operation of the above three operations called the basic operation of the one-step character.
Here we define the editing distance of two strings: for two strings A and B. Through the above basic operation. We can change A to B or B to a, so the minimum number of basic character steps required for string A into string B is called the editing distance of string A and string B.


For example: A= "ABC", b= "CBCD", the editing distance of A and B is 2.
Your task is to compile a high-speed program to calculate the editing distance of randomly two strings. The input input includes multiple sets of test data.

A row of test data for each group, string A and string B.


The length of the string is not greater than 1024. And they are all letters.

Output editing distance. Demo sample Input

ABC CBCD
Demo sample Output
2
Hint Source ZjgsuFrom a string to B string, three operations, written as two-dimensional DP, Dp[i][j] represents a string matching to the i,b string matching to J total number of steps required.

suppose s1[i] = = S2[j] So directly from the dp[i-1][j-1] to get. Or dp[i-1][j]+1 represents the addition of a character, dp[i][j-1]+1 represents the deletion of one character from theassumptions are not the same. Then, as with the above, only the change for dp[i-1][j-1]+1 represents a character change. #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace Std;
Char s1[1200], s2[1200];
int dp[1200][1200];
int main ()
{
int I, J, L1, L2;
while (scanf ("%s%s", S1, S2)!=eof)
{
Memset (Dp,0,sizeof (DP));
L1 = strlen (S1);
L2 = strlen (s2);
for (i = 0; I <= L1; i++)
Dp[i][0] = i;
for (j = 0; J <= L2; j + +)
DP[0][J] = j;
for (i = 1; I <= L1; i++)
{
for (j = 1; J <= L2; j + +)
{
if (s1[i-1] = = S2[j-1])
Dp[i][j] = min (dp[i-1][j-1],min (dp[i-1][j]+1,dp[i][j-1]+1));
Else
Dp[i][j] = min (dp[i-1][j-1]+1,min (dp[i-1][j]+1,dp[i][j-1]+1));
}
}
printf ("%d\n", Dp[l1][l2]);
}
return 0;
}

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

sdutoj1225--Editing Distance (DP: string conversion)

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.