Problem description:
Many programs use strings in large quantities. For different strings, we hope to be able to determine their similar programs. We define a set of operation methods to make the two strings different from each other the same. The specific operation method is:
1. modify a character (for example, replace "A" with "B ");
2. Add a character (for example, change "abdd" to "aebdd ");
3. delete a character (for example, change "traveling" to "traveling ");
For example, for the "abcdefg" and "abcdef" strings, we think we can increase/decrease a "G" to achieve the goal. The above two solutions require only one
Times. The number of times required for this operation is defined as the distance between two strings, and the similarity is equal to the reciprocal of "distance + 1. That is to say, 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?