首页 > 代码库 > USACO hamming 继续暴搜

USACO hamming 继续暴搜

USER: Kevin Samuel [kevin_s1]
TASK: hamming
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.003 secs, 3504 KB]
   Test 2: TEST OK [0.005 secs, 3504 KB]
   Test 3: TEST OK [0.008 secs, 3504 KB]
   Test 4: TEST OK [0.008 secs, 3504 KB]
   Test 5: TEST OK [0.008 secs, 3504 KB]
   Test 6: TEST OK [0.005 secs, 3504 KB]
   Test 7: TEST OK [0.008 secs, 3504 KB]
   Test 8: TEST OK [0.005 secs, 3504 KB]
   Test 9: TEST OK [0.005 secs, 3504 KB]
   Test 10: TEST OK [0.008 secs, 3504 KB]
   Test 11: TEST OK [0.008 secs, 3504 KB]

All tests OK.

YOUR PROGRAM (‘hamming‘) WORKED FIRST TIME! That‘s fantastic -- and a rare thing. Please accept these special automated

congratulations.

/*
ID:kevin_s1
PROG:hamming
LANG:C++
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <bitset>
#include <cmath>

using namespace std;

//gobal variable====
int N, B, D;
vector<bitset<8> > result;//bitset可以加速
string str = "11111111";
bitset<8> last(str);
int MaxDeep;
//==================


//function==========

int hammingDis(bitset<8> a, bitset<8> b){
	bitset<8> x = a xor b;
	return x.count();
}

bool check(bitset<8> bit){//判断是否与前面的数距离小于D
	for(int i = 0; i < result.size(); i++){
		if(hammingDis(bit, result[i]) < D)
			return false;
	}
	return true;
}

void DFS(int deep, int count){//deep为搜索的数字,count为确认合法数字的个数,从小往大搜
	if(deep > MaxDeep)
		return;
	if(count > N)
		return;
	//
	bitset<8> tmp(deep + 1);
	//int dist = hammingDis(last, tmp);
	if(/*dist >= D*/check(tmp) == true){
		result.push_back(tmp);
		last = tmp;
		DFS(deep + 1, count + 1);
	}
	else{
		DFS(deep + 1, count);
	}
	return;
}
//==================

int main(){
	freopen("hamming.in","r",stdin);
	freopen("hamming.out","w",stdout);
	cin>>N>>B>>D;
	MaxDeep = (int)pow(2.0, (double)B) - 1;
	DFS(-1, 0);
	for(int i = 0; i < N; i++){
		cout<<result[i].to_ulong();
		if(i != N - 1){
			if(i % 10 == 9)
				cout<<endl;
			else
				cout<<" ";
		}
	}
	cout<<endl;
	return 0;
}