USACO2.1.5--Hamming Codes
2013-02-03 22:02
302 查看
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.
View Code
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 题解:赤果果的枚举+位运算啊。枚举范围为 [0,2^B-1],先对两个编码进行异或运算得到一个新数字,然后对数字进行与运算和移位运算把其中的1的个数计算出来,再判断Hamming距离是否大于D,如果是则是符合要求的([b]必须与其他所有的数相比,Hamming距离都符合要求,这个数才正确)。 [/b]
View Code
/* ID:spcjv51 PROG:hamming LANG:C */ #include<stdio.h> int n,a[500]; int distance(int x) { int ans; ans=0; while(x) { ans+=x&1; x>>=1; } return ans; } void print() { int i; for(i=1;i<n;i++) { if(i%10==0) printf("%d\n",a[i]); else printf("%d ",a[i]); } printf("%d\n",a[i]); } int main(void) { freopen("hamming.in","r",stdin); freopen("hamming.out","w",stdout); int b,d,ans,i,j,k,x,y; scanf("%d%d%d",&n,&b,&d); for(i=0; i<(1<<b)-1; i++) { ans=1; a[ans]=i; for(j=i+1; j<(1<<b); j++) { x=i^j; if(distance(x)>=d) { for(k=1; k<=ans; k++) { y=j^a[k]; if(distance(y)<d) break; } if(k==ans+1) { ans++; a[ans]=j; } } if(ans==n) break; } if(ans==n) { print(); break; } } return 0; }
相关文章推荐
- USACO2.1.5 Hamming Codes (hamming)
- usaco 2.1.5 Hamming Codes
- USACO:2.1.5 Hamming Codes 汉明码
- [USACO 2.1.5] Hamming Codes
- USACO 2.1.5 Hamming Codes
- USACO 2.1.5 Hamming Codes
- Hamming Codes_usaco2.1.5_codevs1454
- USACO 2.1.5 Hamming Codes
- USACO2.1.5 Hamming Codes (hamming)
- USACO 2.1.5 Healthy Holsteins
- USACO 2.1.5 Hamming Code
- USACO 2.1 Hamming Codes
- USACO sec2.1 Hamming Codes
- USACO Section 2.1: Hamming Codes
- 【USACO 2.1】Hamming Codes
- USACO 2.1 Hamming Codes
- USACO-Section2.1 Hamming Codes【暴力枚举】
- USACO Section 2.1 Hamming Codes (dfs)
- 2.1.5---Hamming Codes
- usaco 2.1.5 hamming