我希望获得具有最小汉明距离的特定长度/字母的所有单词。
我不是数学家,所以我试图展示一个例子来说明我的需要。
Word Length L = 4
Word Alphabet A = {0,1}
Word Alphabet Size |A| = 2
Word Min. Hamming Dist. H = 2
第一个字是
w0 = {0,0,0,0}
现在其他的话可能是:
w1 = {0,0,1,1}
w2 = {1,1,0,0}
汉明距离
Hamming(w0,w1) = 2 >= H
Hamming(w0,w2) = 2 >= H
Hamming(w1,w2) = 4 >= H
如您所见,所有汉明距离都等于或大于H.
出于我的目的,我需要L=512 and A={0,1} and |A|=2 and H=70
。
如何在没有蛮力/尝试和错误的情况下计算这一切?
下面是一些丑陋的快速黑客代码,最终会很慢。如何快速获得这种闪电?
// std header(s)
#include <array>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <iterator>
#include <random>
#include <chrono>
#include <boost/iterator/zip_iterator.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <bitset>
#include <fstream>
#include <iostream>
class Hamming {
public:
Hamming(int & _distance) : distance_(_distance) {};
void operator() ( boost::tuple<int, int> const & words) {
if(boost::get<0>(words)!=boost::get<1>(words)) {
std::bitset<4> n=boost::get<0>(words);
std::bitset<4> m=boost::get<1>(words);
distance_+=(n ^ m).count();
}
}
private:
int & distance_;
};
int main(int argc,char** argv) {
const int arraysize=61;
std::array<int,arraysize> a;
int distance=0;
int num;
std::cout << "Input number of bag_words"<<std::endl;;
std::cin>>num;
std::vector<std::array<int, arraysize>> bag;
Hamming myobject(distance);
////
std::mt19937 mt(std::chrono::high_resolution_clock::now().time_since_epoch().count());
std::uniform_int_distribution<int> d(0, 255);
std::generate(a.begin(),a.end(),[&]{return d(mt);});
bag.push_back(a);
int counter=1;
while (counter<num) {
std::generate(a.begin(),a.end(),[&]{return d(mt);});
int test=0;
distance=0;
for (int i=0 ; i<counter ; i++) {
std::for_each(
boost::make_zip_iterator(
boost::make_tuple(a.begin(), bag[i].begin())
),
boost::make_zip_iterator(
boost::make_tuple(a.end(), bag[i].end())
),
Hamming(distance)
);
if(distance>10) {
++test;
}
else break;
}
if (test==counter) {
bag.push_back(a);
++counter;
}
}
std::cout << std::endl;
std::ofstream myfile ("example.txt");
if (myfile.is_open()) {
for(int i = 0; i < counter; i ++) {
for (int j=0 ; j<arraysize ; j++) {
myfile << bag[i][j] << " " ;
}
myfile << "\n";
}
myfile.close();
}
else std::cout << "Unable to open file";
}