hamming distance(汉明距离)
看knn算法时无意间发现这个算法,但是维基上有错误的示例和python代码。。。因为汉明距离并不是求相同长度字符串(或相同长度的整数)之间的字符(或数位)差异个数。
正确的详见:https://en.wikipedia.org/wiki/Talk:Hamming_distance
然而,我发现百度百科和一些博客都是参考的汉明距离-维基百科,所以都有错 = =。。。
认真分析正确代码后,我认为汉明距离指的是将两个字符串或两个整数编码为一组二进制数,然后计算两二进制bit之间的差异个数。
给一个维基上的正确的代码吧,省的大家麻烦:
s1 = 'karolin'
s2 = 'kerstin' def hamming_distance(s1, s2):
b1, b2 = bytearray(s1, encoding='utf8'), bytearray(s2, encoding='utf8')
diff = 0
for i in range(len(b1)):
if b1[i] != b2[i]:
diff += bin(b1[i] ^ b2[i]).count("1")
return diff print(hamming_distance(s1, s2))
C/C++:
#include <iostream>
#define ull unsigned long long
int hamming_distance(ull x, ull y)
{
int dist = 0;
ull or = x ^ y;
while (or)
{
dist++; or &= or - 1;
}
return dist;
}
int main()
{
ull x, y;
while (std::cin >> x >> y)
std::cout << "最小汉明距离为:" << hamming_distance(x, y) << std::endl;
return 0;
}
以C代码为例,比如两个整数:1, 2,他们的二进制分别为 001, 010 ,从 001 → 010 最少需要2步替换,也就是最小汉明距离为2;又比如两整数:12, 34,他们的二进制分别为 001100, 100010,001100 → 100010 的最小汉明距离为4。
运行示例:
注:第三个可以自己动手验证一下~
然后维基上还有一段代码:
int hamming_distance(unsigned x, unsigned y)
{
return __builtin_popcount(x ^ y);
}
//if your compiler supports 64-bit integers
int hamming_distance(unsigned long long x, unsigned long long y)
{
return __builtin_popcountll(x ^ y);
}
好像我的电脑上不行,没有__builtin_popcountll()函数。
hamming distance(汉明距离)的更多相关文章
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] 461. Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 461. Hamming Distance(汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 477 Total Hamming Distance 汉明距离总和
两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量.计算一个数组中,任意两个数之间汉明距离的总和.示例:输入: 4, 14, 2输出: 6解释: 在二进制表示中,4表示为0100,14表 ...
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [Swift]LeetCode461. 汉明距离 | Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [Swift]LeetCode477. 汉明距离总和 | Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- 12.Hamming Distance(汉明距离)
Level: Easy 题目描述: The Hamming distance between two integers is the number of positions at which th ...
- [LeetCode] 477. Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
随机推荐
- 己亥清爽恢复系列之数据文件1篇:SYSTEM物理损坏或丢失(关键表空间)
己亥清爽系列说明:清爽系列是作为恢复系列的基础篇,基于FS(File System)文件系统的手工还原恢复,也叫基于用户管理的还原恢复.来自于博客园AskScuti 实验说明:在有完全备份基础下,物理 ...
- CentOS 7 yum配置阿里云镜像(转)
1.下载源配置 凡是下载国外的软件,比如用npm,pip,yum有时下载速度感人,最好配置国内镜像地址 yum配置阿里云镜像参考:https://blog.csdn.net/hnmpf/article ...
- JS点击显示隐藏内容
JS点击显示隐藏密码 思路:获取元素,判断点击,如果DIV显示就隐藏,如果DIV隐藏就显示出来. 1 if(DIV是显示的){ 2 div.style.display='none'; 3 } 4 el ...
- 【StarUML】 活动图
StarUML中的活动图本质上是流程图,活动图相对来说,更加专业,它有对信号的处理,对状态动作.数据区别表示,使得更清晰地了解控制流的走向. 1.基本元素 a.活动状态图(Activity).动作状态 ...
- DVWA全级别之File Inclusion(文件包含)
File Inclusion File Inclusion,意思是文件包含(漏洞),是指当服务器开启allow_url_include选项时,就可以通过php的某些特性函数(include(),req ...
- @RequestMapping(value = {"list", ""})
https://www.cnblogs.com/tongs/p/7486478.html @RequestMapping是请求路径的注解 里面写两个value就是,路径可以是这两个, 第二个空,是 ...
- springboot引入Oracle依赖
最近学习spring boot,在网上找一些项目学习,有的项目引入了oracle驱动包,自己搭建一直不成功,百度发现说是权限问题无法下载. 然后参考下面博客终于解决:springboot引入Oracl ...
- 普及C组第四题(8.9)
2298. [noip普及组2T4]异或 (File IO): input:gcdxor.in output:gcdxor.out 题目描述 SarvaTathagata是个神仙,一天他在研究数论时, ...
- 第二十七篇 玩转数据结构——集合(Set)与映射(Map)
1.. 集合的应用 集合可以用来去重 集合可以用于进行客户的统计 集合可以用于文本词汇量的统计 2.. 集合的实现 定义集合的接口 Set<E> ·void add(E) ...
- java学生成绩管理系统
信1805-1 20183590 田庆辉 石家庄铁道大学 2019 年秋季 ...