Hamming Distance

Description

Have 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 that computes this distance for two binary strings.

Input

The input contains several test cases. Each test case consists of two lines. Each line contains one binary number. Any two numbers given in one test case have the same length, which is at most 100 binary digits. The last test case is followed by a line containing the uppercase letter "X".

Output

Your program must output a single line for each test case. The line should contain the statement "Hamming distance is X.", where X is the number of positions where the two numbers have different digits.

Sample Input

0
1
000
000
1111111100000000
0000000011111111
101
000
X

Sample Output

Hamming distance is 1.
Hamming distance is 0.
Hamming distance is 16.
Hamming distance is 2. 题解:读取两个相同长度字符串,从第一位往后对比,如果不一样,ans++。X结束。
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
//#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
char a[],b[];
while(cin>>a&&a[]!='X')
{
cin>>b;
int ans=;
for(int i=,s=strlen(a);i<s;i++)
{
if(a[i]!=b[i])ans++;
}
printf("Hamming distance is %d.\n",ans);
}
return ;
}

最新文章

  1. SQL编程
  2. MySQL表结构及数据的备份
  3. linux 查看磁盘剩余命令
  4. 在.net中设置debug模式预设代码
  5. Webform和MVC,为什么MVC更好一些?
  6. c# 绘图常用对象和方法
  7. 【Unity Shaders】Transparency —— 使用alpha通道创建透明效果
  8. 解决Jenkins运行robot framework selenium脚本不打开浏览器的问题
  9. vue 前端框架 (三)
  10. Hbase获取流程
  11. 【转】WPF自定义控件与样式(10)-进度控件ProcessBar自定义样
  12. Subverion仓库迁移知识点整理
  13. B - Red and Black 问题思考
  14. CentOS 5.5 虚拟机安装 VirtualBox 客户端增强功能
  15. 纯css3实现的创意图片放大镜
  16. [AT2364] [agc012_d] Colorful Balls
  17. ie6下png图片背景色处理
  18. Android 按钮长按下去重复执行某个动作,放开后停止执行动作
  19. Python下的正则表达式原理和优化笔记
  20. Shell 命令挂后台执行

热门文章

  1. LeetCode 213. House Robber II
  2. ibatis动态修改select出来的字段
  3. centos安装ganttproject
  4. jquery+ajax 实现text框模糊搜索并可利用listbox实时显示模糊搜索列表结果
  5. Java实现二叉树先序,中序,后序遍历
  6. react总结
  7. Python ---------copy
  8. 安装redis,以及python如何引用redis
  9. centos6.5安装python3
  10. instanceof运算符、Class的isInstance( )与isAssignableFrom之间的区别