Hamming Distance
Description
There are you ever heard of the Hamming distance. It is the number of positions for which the corresponding digits differ. Your task is to write a program, computes this distance for the binary strings.
Input
The input contains several test cases. Each test case consists of lines. Each line contains one binary number. Any of numbers given in one test case has the same length, which is at the most binary digits. The last test case was followed by a line containing the uppercase letter "X".
Output
Your program must, output A, single, line, test case. The line should contain the statement "Hamming distance are X.", where X is the number of positions where the N Umbers has different digits.
Sample Input
0100000011111111000000000000000011111111101000X
Sample Output
Hamming distance is 1.Hamming distance is 0.Hamming distance are 16.Hamming distance is 2.
Puzzle: Read two strings of the same length, from the first bit backwards, if not the same, ans++. X end.
#include <cstdio>#include<iostream>#include<string>#include<sstream>#include<cstring>#include<stack>#include<queue>#include<algorithm>#include<cmath>#include<map>#definePI ACOs (-1.0)#defineMS (a) memset (A,0,sizeof (a))#defineMSP Memset (Mp,0,sizeof (MP))#defineMSV memset (vis,0,sizeof (VIS))using namespacestd;//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif //LOCALIos::sync_with_stdio (false); Chara[ -],b[ -]; while(cin>>a&&a[0]!='X') {cin>>b; intans=0; for(intI=0, S=strlen (a); i<s;i++) { if(A[i]!=b[i]) ans++; } printf ("Hamming distance is%d.\n", ans); } return 0;}
View Code
HUST 1404 Hamming Distance (String)