Similarity algorithm, java instance 9-Hamming distance Hamming Distance

Source: Internet
Author: User

Java Instance 9-Hamming distance Hamming Distance

http://blog.csdn.net/kindterry/article/details/6581344

/** in information theory, the Hamming distance between two equal-length strings
* is the number of different characters in the corresponding position of two strings,
* In other words, Hamming distance is the length of the character to replace a string with another string.
* For example, the Hamming distance between 1011101 and 1001001 is 2,
The Hamming distance between *toned and roses is 3.
* Hamming weight is the Hamming distance of a string of 0 strings relative to the same length,
* That is, it is the number of non-zero elements in the string: for a binary string, it is the number of 1,
* So the Hamming weight of 11101 is 4.
* The following code shows how Hamming distance and Hamming weight are calculated in Java.
*/
Package re;
public class Hammingdistance {
public static void Main (string[] args) {
String str1 = "ABCDEFG";
String str2 = "Aacceeg";
Hammingdistance HD = new Hammingdistance ();
int distance = Hd.getdistance (str1, str2);
System.out.println ("Distance is" + distance);
int weight = hd.getweight (255);
System.out.println ("Weight is" + weight);
}
/**
* Calculate Hamming Distance between, strings
*
* @author
* @param str1 the 1st string
* @param str2 the 2nd string
* @return Hamming Distance between STR1 and str2
*/
public int Getdistance (string str1, String str2) {
int distance;
if (str1.length () = Str2.length ()) {
Distance =-1;
} else {
Distance = 0;
for (int i = 0; i < str1.length (); i++) {
if (Str1.charat (i)! = Str2.charat (i)) {
distance++;
}
}
}
return distance;
}
/**
* Calculate Hamming weight for binary number
* @author
* @param i the binary number
* @return Hamming weight of the binary number
*/
public int getweight (int i) {
int n;
for (n = 0; i > 0; n++) {
I &= (i-1);
}
return n;
}
}

Similarity algorithm, java instance 9-Hamming distance Hamming Distance

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.