Tzoj 1072: editing distance (Dynamic Planning)

Source: Internet
Author: User

1072: editing distance

Time limit (Common/Java): 1000 ms/10000 ms memory limit: 65536 Kbyte
Total submissions: 917 pass the test: 275

Description

Assume that the basic operations of a string are only: delete one character, insert one character, and modify one character to another.
We call any of the above three operations a step-by-step basic operation.
Next we define the editing distance between two strings: For two strings a and B, through the above basic operations, we can convert A to B or B to, the minimum number of operation steps required for converting string a to string B is the distance between string a and string B.
For example, if a = "ABC", B = "cbcd", the distance between A and B is 2.
Your task is to compile a quick program to calculate the editing distance between any two strings.

Input

The input contains multiple groups of test data. One row of test data in each group, which is string a and string B.
The length of a string is not greater than 1024 characters, and all characters are letters.

Output

Editing distance.

Sample Input

ABC cbcd

Sample output

2

Prompt

I believe that the dynamic planning algorithm can solve this problem, because I did this. Pai_^

Question Source

Zjgsu

 

# Include <stdio. h> # include <string. h> int DP [1029] [1029]; int main () {char str1 [1029], str2 [1029]; while (scanf ("% S % s", str1, str2)> 0) {int len1, len2; len1 = strlen (str1); len2 = strlen (str2); For (INT I = 0; I <= len2; I ++) // initialize DP [0] [I] = I; for (INT I = 0; I <= len1; I ++) DP [I] [0] = I; for (INT I = 1; I <= len1; I ++) for (Int J = 1; j <= len2; j ++) if (str1 [I-1] = str2 [J-1]) {DP [I] [J] = DP [I-1] [J-1];} else {DP [I] [J] = DP [I-1] [J] + 1; // remove character str1 [I] If (DP [I] [J-1] + 1 <DP [I] [J]) DP [I] [J] = DP [I] [J-1] + 1; // Add character str2 [J] To str1 if (DP [I-1] [J-1] + 1 <DP [I] [J]) DP [I] [J] = DP [I-1] [J-1] + 1; // converts str1 [I] To str2 [J]} printf ("% d \ n ", DP [len1] [len2]);}


 

Tzoj 1072: editing distance (Dynamic Planning)

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.