15 thinking question editing distance

Source: Internet
Author: User

 

/* Description: Set A and B to two strings. Use the minimum number of characters to convert string a to string B. The character operations mentioned here include: (1) deleting one character; (2) Inserting one character; (3) changing one character to another. The minimum number of characters used to convert string a to string B is the distance from string a to string B and is recorded as D (A, B ). Try to design an effective Algorithm For any two strings a and B, calculate their editing distance D (A, B ). Requirement: input: the first line is string a, and the second line is string B. Output: string a and B editing distance D (A, B) idea: open a two-dimensional array d [I] [J] to record the editing distance between a0-ai and b0-bj, to implement recursion, you need to consider the overhead of the delete, insert, and replace operations on one of the strings, and find out a minimum overhead that is the specific algorithm: first, specify the first row and the first column, and then calculate each value D [I, j] as follows: d [I] [J] = min (d [I-1] [J] + 1, D [I] [J-1] + 1, d [I-1] [J-1] + (S1 [I] = S2 [J]? ); In the last row, the value in the last column is the minimum editing distance */# include <iostream> using namespace STD; char S1 [100], S2 [100]; int DP [101] [101]; int min (int A, int B, int c) {int temp = (a <B )? A: B; Return (temp <c )? Temp: C;} void levenshteindistance (INT len1, int len2) {for (INT I = 0; I <= len1; I ++) DP [I] [0] = I; for (Int J = 0; j <= len2; j ++) DP [0] [J] = J; for (INT I = 1; I <= len1; I ++) {for (Int J = 1; j <= len2; j ++) {int cost = (S1 [I-1] = S2 [J-1])? 0: 1; // note that this is not S1 [I] = S2 [J] int deletion = DP [I-1] [J] + 1; int insertion = DP [I] [J-1] + 1; int substitution = DP [I-1] [J-1] + cost; DP [I] [J] = min (deletion, insertion, substitution) ;}} cout <DP [len1] [len2] <Endl;} void main () {cout <"Enter string 1:" <Endl; cin> S1; cout <"Enter string 2:" <Endl; CIN> S2; levenshteindistance (strlen (S1), strlen (S2 ));}

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.