USACO 2.1 Hamming Codes

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):

        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 题目大意:就是给你NBD三个正整数,意思是在0到2^B-1中选择N个数字,而且两两之间“距离”大于D。对“距离”是这么定义的,二进制对应位不同符号的个数。
思路:说实话是没有什么思路的,有想搜索但是好虚,觉得数据不小(选或者不选,复杂度是2^(2^B-1)),不敢写,后来上网看了解析,居然就是搜索,还是最裸的那种。。。有点想不明白,不过也提醒我,撑死胆大的,饿死胆小的。代码如下
 /*
ID:fffgrdc1
PROB:hamming
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
using namespace std;
int n,m,d,cnt=;
int topnum=;
int ans[]={};
bool check(int x,int y)
{
x=x^y;int tot=;
while(x)
{
if(x&)tot++;
x>>=;
}
return tot>=d;
}
void dfs(int x)
{
if(cnt==n)
{
int num=;
for(int i=;i<=cnt;i++)
{
num++;
if(num==)
printf("%d",ans[i]);
else printf(" %d",ans[i]);
if(num==)
{
printf("\n");
num=;
}
}
if(num)printf("\n");
exit();
}
if(x>topnum)return;
int flag=;
for(int i=;i<=cnt;i++)
{
if(!check(ans[i],x))
{
flag=;
break;
}
}
if(flag)
{
ans[++cnt]=x;
dfs(x+);
cnt--;
}
dfs(x+); }
int main()
{
freopen("hamming.in","r",stdin);
freopen("hamming.out","w",stdout);
scanf("%d%d%d",&n,&m,&d);
while(m--)topnum<<=;
topnum--;
//printf("%d\n",topnum);
dfs();
return ;
}
上一篇:权限管理&用户组管理


下一篇:2016年11月7日 星期一 --出埃及记 Exodus 19:23