USACO 2.1.5 – Hamming Codes

#include <algorithm>
#include <cstdio>
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>

using namespace std;

int main() {
	freopen("hamming.in", "r", stdin), freopen("hamming.out", "w", stdout);
	int B, D, a = -1;size_t i, N;
	scanf("%d %d %d", &N, &B, &D);
	vector<int> v;
	while (++a < (1 << B) && v.size() != N) {
		for (i = 0; i < v.size();  i++)
			if (__builtin_popcount(a ^ v[i]) < D)
				break;
		if (i == v.size())
			v.push_back(a);
	}
	for(i=0; i<v.size(); i+= 10){
		stringstream  s;
		copy(v.begin() +i ,v.begin()+i +min(10,int(v.size()-i)), ostream_iterator<int>(s," "));
		puts(s.str().erase(s.str().length()-1).c_str());
	}
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: