USACO Section 2.1: Hamming Codes

挺简单的一道题

 1 /*

 2 ID: yingzho1

 3 LANG: C++

 4 TASK: hamming

 5 */

 6 #include <iostream>

 7 #include <fstream>

 8 #include <string>

 9 #include <map>

10 #include <vector>

11 #include <set>

12 #include <algorithm>

13 #include <stdio.h>

14 #include <queue>

15 #include <cstring>

16 

17 using namespace std;

18 

19 ifstream fin("hamming.in");

20 ofstream fout("hamming.out");

21 

22 int N, B, D;

23 

24 int hamming(int l, int r) {

25     int tmp = l ^ r;

26     int ret = 0;

27     while (tmp) {

28         if (tmp & 1) ret++;

29         tmp >>= 1;

30     }

31     return ret;

32 }

33 

34 int main()

35 {

36     fin >> N >> B >> D;

37     int acount = 1;

38     vector<int> words;

39     words.push_back(0);

40     for (int num = 0; num < (1 << B); num++) {

41         if (acount == N) break;

42         int j = 0;

43         for (; j < words.size(); j++) {

44             if (hamming(num, words[j]) < D) break;

45         }

46         if (j == words.size()) {

47             words.push_back(num);

48             acount++;

49         }

50     }

51     for (int i = 0; i < N; i++) {

52         fout << words[i];

53         if (i % 10 != 9 && i != N-1) fout << " ";

54         else fout << endl;

55     }

56 

57     return 0;

58 }

 

你可能感兴趣的:(USACO)