The beauty of programming 3.3 calculates the similarity between two strings, and the beauty 3.3

Source: Internet
Author: User
Tags first string

The beauty of programming 3.3 calculates the similarity between two strings, and the beauty 3.3

If there are two strings: abcd and bbcd, the numbers of different strings are 1, that is, the first character is different, defines the similarity of strings as 1/(x + 1). x indicates the number of different characters.

We can compare the numbers of different characters in two strings in three ways:

1. Remove the different characters in the first string and compare the next character at the same time.

2. Remove the different characters in the second string and compare the next character at the same time.

3. Remove the different characters in the string and compare the next character at the same time.

Then, we need to add 1 to the number of different characters. Finally, we only need to obtain the one with the smallest median of the three methods.

It is easy to find whether to compare the characters in sequence in the same time for Recursive operations. Therefore, the solution to this problem is as follows:

Function declaration:

/* 3.3 calculate the similarity between two strings */int DutCalculateStringDistance (std: string &, std: string &); int DutCalculateStringDistance (std: string &, int, int, std: string &, int, int );

Source code:

Int DutCalculateStringDistance (string & s1, int begin1, int end1, string & s2, int begin2, int end2) {if (begin1> end1) {if (begin2> end2) return 0; elsereturn end2-begin2 + 1;} if (begin2> end2) {if (begin1> end1) return 0; elsereturn end1-begin1 + 1 ;} if (s1 [begin1] = s2 [begin2]) return DutCalculateStringDistance (s1, begin1 + 1, end1, s2, begin2 + 1, end2 ); else {/* calculate similarity of Different Methods */int t1 = DutCalculateStringDistance (s1, begin1 + 1, end1, s2, begin2, end2); int t2 = DutCalculateStringDistance (s1, begin1, end1, s2, begin2 + 1, end2); int t3 = DutCalculateStringDistance (s1, begin1 + 1, end1, s2, begin2 + 1, end2 ); /* obtain a minimum similarity */return DutMin <int> (t1, t2, t3) + 1 ;}} int DutCalculateStringDistance (string & s1, string & s2) {return DutCalculateStringDistance (s1, 0, (int) s1.length ()-1, s2, 0, (int) s2.length ()-1 );}

Template functions used in the Code:

template <typename T>T DutMin(T data1, T data2){return data1 < data2 ? data1 : data2;}
template <typename T>T DutMin(T data1, T data2, T data3){return DutMin<T>(DutMin<T>(data1, data2), data3);}





Who can provide an algorithm for calculating the similarity between two strings in C #?

Calculate the length of a first, and then carry out the length-1 loop. in the loop, 2 characters are extracted each time, and then compared with B and indexof. If the value is not equal to-1, that is, no, then the loop continues. If it is not equal to-1, it is connected to three characters, and so on (this can be done in the same way, saves code ). it can be seen that only A few characters are available, and the length of the entire A is divided to obtain the similarity.
 
Anyone who has two string similarity algorithms and code, C ++ or C, urgent, 657256758 @ qqcom, will be rewarded

# Include <stdio. h>
# Include <string. h>
Void main ()
{
Char s1 [50], s2 [50];
Int I;
Gets (s1 );
Gets (s2 );
For (I = 0; s1 [I]! = '\ 0' & s2 [I]! = '\ 0'; I ++)
If (s1 [I]! = S2 [I]) break;
If (I = strlen (s1) & I = strlen (s2) printf ("s1 string and s2 string are completely equal \ n ");
Else if (s1 [I] <s2 [I]) printf ("s1 string less than s2 string \ n ");
Else printf ("s1 string greater than s2 string \ n ");
}
 

Related Article

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.