【发布时间】:2017-04-10 07:53:40
【问题描述】:
给定一个位向量v,计算具有v的汉明距离1,然后距离为2的位的集合,直到输入参数t。 p>
所以
011 I should get
~~~
111
001
010
~~~ -> 3 choose 1 in number
101
000
110
~~~ -> 3 choose 2
100
~~~ -> 3 choose 3
如何有效地计算这个?向量并不总是 3 维,例如它可能是 6。这将在我的真实代码中运行很多次,因此也欢迎一些效率(即使支付更多内存)。
我的尝试:
#include <iostream>
#include <vector>
void print(const std::vector<char>& v, const int idx, const char new_bit)
{
for(size_t i = 0; i < v.size(); ++i)
if(i != idx)
std::cout << (int)v[i] << " ";
else
std::cout << (int)new_bit << " ";
std::cout << std::endl;
}
void find_near_hamming_dist(const std::vector<char>& v, const int t)
{
// if t == 1
for(size_t i = 0; i < v.size(); ++i)
{
print(v, i, v[i] ^ 1);
}
// I would like to produce t == 2
// only after ALL the t == 1 results are reported
/* how to? */
}
int main()
{
std::vector<char> v = {0, 1, 1};
find_near_hamming_dist(v, 1);
return 0;
}
输出:
MacBook-Pro:hammingDist gsamaras$ g++ -Wall -std=c++0x hammingDist.cpp -o ham
MacBook-Pro:hammingDist gsamaras$ ./ham
1 1 1
0 0 1
0 1 0
【问题讨论】:
-
我 recently 已经回答了这个问题,差不多,尽管你提出的问题不同。
-
@harold 是的,因为它略有不同! :)
标签: c++ algorithm machine-learning bit-manipulation hamming-distance