USACO section 2.1 Hamming Codes(直接枚举暴搜)
2012-08-22 10:46
344 查看
Hamming Codes
Rob Kolstad
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):
Since five bits were different, the Hamming distance is 5.
刚拿到题还不知道怎办,有想过暴搜,但怕暴,百度了下,发现真可以暴搜,就直接搜了明显是数据太弱了,
要是输入 126 30 6 就很久没反应了。
Hamming Codes
Brian Jacokes
There are only a few tools we need to solve this problem. First of all, we can use basic techniques to find the Nth bit of a number M: counting the least significant bit as bit 0, the next bit as bit 1, etc., we can find the value of that bit through the
expression
In other words, we are shifting the number 1 left by N spaces, and then performing a binary AND on the resulting number with our original number, which will leave either a 1 in the Nth bit or a 0. So the first thing we have to do is find out the distance
between each pair of numbers within the set of all numbers with B bits (0..2B-1).
We also know that 0 can be one of the numbers in the set, because if the minimum number in the set were N instead of 0, we could XOR N to each number in the set and they would still be the same distance apart. The limits on the problem are low enough that
we can do a DFS, and as soon as we hit a solution we can output it and exit.
Here are the test data inputs:
Keep up the good work!
Thanks for your submission!
Rob Kolstad
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 lineSAMPLE 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
刚拿到题还不知道怎办,有想过暴搜,但怕暴,百度了下,发现真可以暴搜,就直接搜了明显是数据太弱了,
要是输入 126 30 6 就很久没反应了。
/* ID:nealgav1 PROG:hamming LANG:C++ */ #include<fstream> using namespace std; const int mm=1<<8; const int oo=1e9; int a,b,c,len; int hamm[79],lham; int hamming_length(int x,int y) { int z=x^y;int ans=0; while(z>0) { if(z&1) ans++; z>>=1; } return ans; } void dfs(int dep) { if(dep==a)return; int i,j; for(i=1;i<len;i++) { bool flag=0; for(j=0;j<lham;j++) if(hamming_length(i,hamm[j])<c){flag=1;break;} if(!flag){hamm[j]=i,lham++;break;} } dfs(dep+1); } int main() { ifstream cin("hamming.in"); ofstream cout("hamming.out"); cin>>a>>b>>c; len=1<<b;lham=1;hamm[0]=0; dfs(1); cout<<"0"; for(int i=1;i<lham;i++) if(i%10==0)cout<<"\n"<<hamm[i]; else cout<<" "<<hamm[i]; cout<<"\n"; return 0; }
Hamming Codes
Brian Jacokes
There are only a few tools we need to solve this problem. First of all, we can use basic techniques to find the Nth bit of a number M: counting the least significant bit as bit 0, the next bit as bit 1, etc., we can find the value of that bit through the
expression
int Nth_bit = (1 << N) & M;
In other words, we are shifting the number 1 left by N spaces, and then performing a binary AND on the resulting number with our original number, which will leave either a 1 in the Nth bit or a 0. So the first thing we have to do is find out the distance
between each pair of numbers within the set of all numbers with B bits (0..2B-1).
We also know that 0 can be one of the numbers in the set, because if the minimum number in the set were N instead of 0, we could XOR N to each number in the set and they would still be the same distance apart. The limits on the problem are low enough that
we can do a DFS, and as soon as we hit a solution we can output it and exit.
#include <stdio.h> #include <stdlib.h> #include <iostream.h> #define MAX (1 << 8 + 1) #define NMAX 65 #define BMAX 10 #define DMAX 10 int nums[NMAX], dist[MAX][MAX]; int N, B, D, maxval; FILE *in, *out; void findgroups(int cur, int start) { int a, b, canuse; char ch; if (cur == N) { for (a = 0; a < cur; a++) { if (a % 10) fprintf(out, " "); fprintf(out, "%d", nums[a]); if (a % 10 == 9 || a == cur-1) fprintf(out, "\n"); } exit(0); } for (a = start; a < maxval; a++) { canuse = 1; for (b = 0; b < cur; b++) if (dist[nums][a] < D) { canuse = 0; break; } if (canuse) { nums[cur] = a; findgroups(cur+1, a+1); } } } int main() { in = fopen("hamming.in", "r"); out = fopen("hamming.out", "w"); int a, b, c; fscanf(in, "%d%d%d", &N, &B, &D); maxval = (1 << B); for (a = 0; a < maxval; a++) for (b = 0; b < maxval; b++) { dist[a][b] = 0; for (c = 0; c < B; c++) if (((1 << c) & a) != ((1 << c) & b)) dist[a][b]++; } nums[0] = 0; findgroups(1, 1); return 0; }
USER: Neal Gavin Gavin [nealgav1] TASK: hamming LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 3336 KB] Test 2: TEST OK [0.011 secs, 3336 KB] Test 3: TEST OK [0.000 secs, 3336 KB] Test 4: TEST OK [0.000 secs, 3336 KB] Test 5: TEST OK [0.000 secs, 3336 KB] Test 6: TEST OK [0.000 secs, 3336 KB] Test 7: TEST OK [0.000 secs, 3336 KB] Test 8: TEST OK [0.000 secs, 3336 KB] Test 9: TEST OK [0.000 secs, 3336 KB] Test 10: TEST OK [0.000 secs, 3336 KB] Test 11: TEST OK [0.000 secs, 3336 KB] All tests OK. Your program ('hamming') produced all correct answers! This is your submission #2 for this problem. [b]Congratulations!
Here are the test data inputs:
------- test 1 ----
16 7 3------- test 2 ----
2 7 7
------- test 3 ----
3 6 4
------- test 4 ----
5 5 1
------- test 5 ----
10 8 4
------- test 6 ----
20 6 2
------- test 7 ----
32 5 1
------- test 8 ----
50 8 2
------- test 9 ----
60 7 2
------- test 10 ----
16 8 3
------- test 11 ----
15 8 4
Keep up the good work!
Thanks for your submission!
相关文章推荐
- USACO section 2.1 Hamming Codes(直接枚举暴搜)
- USACO-Section2.1 Hamming Codes【暴力枚举】
- USACO Section 2.1 Hamming Codes
- USACO Section 2.1 Hamming Codes
- USACO Section 2.1 Hamming Codes
- USACO Section 2.1: Hamming Codes
- USACO 2.1 Hamming Codes(位运算枚举)
- zoj 1716 又是一道简单题。直接暴力枚举实现!
- UVA 11889 - Benefit 可直接枚举
- Play 2.1 - Evolution插件使用指南 数据库脚本不能直接执行
- USACO 2.1 Hamming Codes
- usaco 2.1 hamming...枚举...
- HNUOJ13302和13308贪心枚举有俩种情况的想办法直接枚举俩种情况每次枚举比较值
- 快照枚举进程/直接枚举进程
- USACO 2.1 Hamming Codes (迭代)
- NOI.openjudge 2.1 算数枚举 7647 easy的余数相同问题
- 枚举也能直接转换为对应的数值输出
- USACO 2.1 Hamming Codes
- UVA 11889 - Benefit 可直接枚举
- USACO section 2.1 Healthy Holsteins(枚举,二进制存储)