Calculating the similarity of strings

Source: Internet
Author: User
Tags modify

After reading the "Beauty of Programming" book "The Similarity of the calculation string", the book finally put forward a little memory and thinking.

Let me repeat the original question here:

The original question description: Many programs use a lot of strings. For different strings, we want to have a way of judging their similar programs. We define a set of operational methods to make the two different strings the same, the specific action method is:

1. Modify a character (e.g. substituting "a" with "B");

2. Add a character (e.g. "ABDD" into "AEBDD");

3. Delete a character (such as "travelling" into "traveling");

For example, for the "ABCDEFG" and "abcdef" two strings, we think we can achieve the goal by adding/reducing a "g". Both of the above options are only needed once. The number of times required for this operation is defined as the distance of two strings, and the similarity equals the reciprocal of "distance +1". In other words, the distance between "ABCDEFG" and "abcdef" is 1 and the similarity is 1/2=0.5.

Given any two strings, can you write an algorithm to calculate their similarity?

Analysis and solution of the original text

It's not hard to see that two strings are definitely no more than their length (we can convert two strings into empty strings by deleting them). Although this conclusion is not helpful to the result, it is at least possible to know that the distance of any two strings is limited.

Let's focus on how we can turn this problem into a smaller, same child problem. If there are two strings A=xabcdae and B=xfdfa, their first character is the same, so long as the a[2,..., 7]=abcdae and b[2,..., 5]=fdfa distance is OK. But if the first character of the two string is not the same, you can do the following (Lena and LenB are the lengths of A and b strings respectively).

1. Deletes the first character of a string and calculates the distance between the a[2,..., LenA] and b[1,..., LenB].

2. Delete the first character of the B string and compute the distance between the a[1,..., LenA] and b[2,..., LenB].

3. Modify the first character of the A string to be the first character of the B string, and then compute the distance between the a[2,..., LenA] and b[2,..., LenB].

4. Modify the first character of the B string to the first character of a string, and then compute the distance between the a[2,..., LenA] and b[2,..., LenB].

5. Increase the first character of the B string before the first character of the A string, and then compute the distance between the a[1,..., LenA] and b[2,..., LenB].

6. Increase the first character of the A string before the first character of the B string, and then compute the distance between the a[2,..., LenA] and b[1,..., LenB].

In this topic, we don't care what the string is after two strings become equal. Therefore, you can combine the above 6 operations into the following:

1. After a one-step operation, the A[2,..., LenA] and b[1,..., LenB] become the phase string.

2. After a one-step operation, the A[2,..., LenA] and b[2,..., LenB] become the phase string.

3. After a one-step operation, the A[1,..., LenA] and b[2,..., LenB] become the phase string.

In this way, a recursive program can be completed quickly.

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.