Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of length B bits (1 <= B <= 8), such that each of the codewords is at least Hamming distance of D (1 <= D <= 7) away from each of the other codewords. The Hamming distance between a pair of codewords is the number of binary bits that differ in their binary notation. Consider the two codewords 0x554 and 0x234 and their differences (0x554 means the hexadecimal number with hex digits 5, 5, and 4):
0x554 = 0101 0101 0100 0x234 = 0010 0011 0100 Bit differences: xxx xx
Since five bits were different, the Hamming distance is 5.
PROGRAM NAME: hamming
INPUT FORMAT
N, B, D on a single line
SAMPLE INPUT (file hamming.in)
16 7 3
OUTPUT FORMAT
N codewords, sorted, in decimal, ten per line. In the case of multiple solutions, your program should output the solution which, if interpreted as a base 2^B integer, would have the least value.
SAMPLE OUTPUT (file hamming.out)
0 7 25 30 42 45 51 52 75 76 82 85 97 102 120 127
출처: http://train.usaco.org/
N,B,D 가 주어졌다고 할 때, N 개의 codewords 를 찾는 문제인데 codewords 란 최대 B bits 로 구성된 숫자로써 codewords는 다른 모든 codewords 와 Hamming distance 가 D 이상입니다. Hamming distance 란 2진수의 두 수에서 다른 비트의 개수를 말합니다.
특별히 최적화를 할 방법이 생각나지 않아 완전 탐색으로 풀어봤습니다.
my solving
c++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include <fstream> #include <iostream> #include <algorithm> #include <vector> #include <set> using namespace std; int getHammingDistance(int a, int b) { int ret = 0; while (a > 0 || b > 0) { if (a % 2 != b % 2) ret++; a /= 2; b /= 2; } return ret; } int main() { ifstream fin("hamming.in"); ofstream fout("hamming.out"); int n, b, d, maxNum; fin >> n >> b >> d; maxNum = (1 << b) - 1; int temp = getHammingDistance(0, 25); set<int> ret; for (int i = 0; i <= maxNum; i++) { ret.clear(); ret.insert(i); for (int j = i; j <= maxNum; j++) { bool ok = true; for (set<int>::iterator it = ret.begin(); it != ret.end(); it++) { if (getHammingDistance(*it, j) < d) { ok = false; break; } } if (ok) { ret.insert(j); if (ret.size() >= n) break; } } if (ret.size() >= n) break; } int i = 0; for (set<int>::iterator it = ret.begin(); it != ret.end(); it++) { if (i != 0 && i % 10 == 0) fout << endl; else if (i != 0) fout << " "; fout << *it; i++; } fout << endl; fin.close(); fout.close(); return 0; } | cs |
'Algorithm, Data structure > Solved Algorithmic Problem' 카테고리의 다른 글
USACO 2.2 - Subset Sums (0) | 2016.07.07 |
---|---|
USACO 2.2 - Preface Numbering (0) | 2016.07.07 |
USACO 2.1 - Healthy Holsteins (0) | 2016.07.06 |
USACO 2.1 - Sorting a Three-Valued Sequence (0) | 2016.07.04 |
USACO 2.1 - Ordered Fractions (0) | 2016.07.04 |