The beauty of programming is 3.3, and the string similarity is calculated.

Source: Internet
Author: User

Two strings are provided. Three operations are performed:

1. modify a character

2. Add one character

3. delete a character

One operation adds 1 to the distance between two strings. through three operations, the two strings are eventually equal, and the minimum number of operations is solved to obtain the similarity between the two strings, similarity is 1/(distance + 1 ). For example, if the distance between abcdefg and abcdef is 1 (delete g through abcdefg), the similarity is 1/2 = 0.5.

Write the program.

Problem Analysis:

When solving the similarity between xabcd and xbde strings, because the first string is equal, it can be proved that the similarity between xabcd and xbde is equal to that of ABCD and BDE, in this way, we can reduce the problem to a shorter string similarity.

What if the first character is not equal? We can make the first character of two strings equal in six ways:

1. Delete the first character of string a and calculate the similarity between string a [2, 3...] and string B [1, 2 ...].

2. Delete the first character of string B and calculate the similarity between string B [2, 3...] and string a [1, 2 ...].

3. Add B [1] to the First of string a, and calculate the similarity between the original A [...] and the original B [...].

4. Add the beginning of string a [1] to string B and calculate the similarity between the original string B [...] and the original string a [2, 3 ...].

5. Change a [1] to B [1] to calculate the similarity between a [2, 3...] and B [2, 3 ...].

5. Change B [1] to a [1] to calculate the similarity between a [2, 3...] and B [2, 3 ...].

In this way, we can use a recursive method to solve the string similarity. The Code is as follows:

/** Bop_3_3.cpp ** created on: 2012-5-24 * Author: ICT */# include <cstdio> # include <cstdlib> # include <cstring> # include <string> # include <algorithm> using namespace STD; # define Max 100int calculate (char * a, char * B) {int L1 = strlen (a); int L2 = strlen (B); // recursive return condition, determine whether the length of string a and string B is 0if (L1 = 0) {If (L2 = 0) return 0; elsereturn L2;} If (L2 = 0) {If (L1 = 0) return 0; elsereturn L1;} if (a [0] = B [0]) // A [0] = B [0], we just calculate the [1, 2...] and the B [1, 2...] distance {return calculate (a + 1, B + 1);} else {int T1 = calculate (a + 1, B); // just delete the [0], or just add the [0] before the B [0] int t2 = calculate (A, B + 1); // just delete the B [0], or just add the B [0] before the [0] int T3 = calculate (a + 1, B + 1 ); // just change the [0] to B [0], or change the B [0] to a [0] Return min (T1, T2), T3) + 1; // return the minimal value} int main () {char a [Max]; char B [Max]; scanf ("% s", ); // input the string ascanf ("% s", B); // input the string bprintf ("% F \ n", 1/(1.0 + calculate (, b); 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.