HUST 1404 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 ;
- }
HUST 1404 Hamming Distance(字符串)的更多相关文章
- 461. Hamming Distance and 477. Total Hamming Distance in Python
题目: The Hamming distance between two integers is the number of positions at which the corresponding ...
- 64. 海明距离(Hamming Distance)
[本文链接] http://www.cnblogs.com/hellogiser/p/hamming-distance.html [介绍] 在信息领域,两个长度相等的字符串的海明距离是在相同位置上不同 ...
- hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
- HDU 4712:Hamming Distance
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- 海明距离hamming distance
仔细阅读ORB的代码,发现有很多细节不是很明白,其中就有用暴力方式测试Keypoints的距离,用的是HammingLUT,上网查了才知道,hamming距离是相差位数.这样就好理解了. 我理解的Ha ...
- hdu 4712 Hamming Distance(随机函数暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
- HDU 472 Hamming Distance (随机数)
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) To ...
- 461. Hamming Distance(leetcode)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- Codeforces 608B. Hamming Distance Sum 模拟
B. Hamming Distance Sum time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
随机推荐
- elasticsearch+spark+hbase 整合
1.用到的maven依赖 <dependency> <groupId>org.apache.spark</groupId> <artifactId>sp ...
- %type的用法
//%type //如果声明的变量是直接映射到数据库的某一列上,那么就可以使用%type关键字将变量 //锚定到这个列上.这样做有什么好处呢? //比如: //declare v_ename scot ...
- 解决Intellij Idea里tomcat启动报The JRE_HOME environment variable is not defined correctly的错误
创建一个maven项目后想启动tomcat测试一下新建的maven项目,结果一直报The JRE_HOME environment variable is not defined correctly, ...
- 安装oracle 10g时提示:操作系统版本: 必须是5.1或者5.2 怎么办?
1.在安装目录中搜索refhost.xml,然后在适当位置添加以下内容,注意括号配对 <!--Microsoft Windows 7--> <OPERATING_SYSTEM> ...
- C# 语言规范_版本5.0 (第17章 特性)
1. 特性 C# 语言的一个重要特征是使程序员能够为程序中定义的实体指定声明性信息.例如,类中方法的可访问性是通过使用 method-modifiers(public.protected.intern ...
- Ansible hostvars
1. inventory hosts file 中的server 变量会覆盖group变量. hostvars: { "iaas_name": "test", ...
- java变量和数据类型总结
- mysql最大连接数问题
进入mysql系统就, 查询最大连接数:show variables like 'max_connections'; 修改最大连接数:set global max_connections=1000;
- C++知识体系
基础知识 推荐书目 C++ <C++程序设计>(课程教材即可,简而薄) <STL源码剖析>(对C++进一步深化,也是必备知识) <C++对象模型>(经典中经典,重点 ...
- drupal7 开发自定义上传、下载模块的上传功能
关键点有两个:1.在页面上显示出上传的控件,2.代码实现文件上传到服务器的功能 一.显示控件: 先来看关键点1: 实现页面显示出上传控件, 关键代码: $form['my_file_field'] = ...