您的位置:首页 > 其它

usaco——hamming

2010-02-18 13:35 218 查看
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

/*
ID: guangxue1
PROG: hamming
LANG: C++
*/

#include <fstream>

using namespace std;

int N,B,D;
int ans[64];

bool Is(int a,int b)
{
int cnt=0;
for(int i=0; i<B; ++i)
{if((a&1)!=(b&1)) ++cnt; a=a>>1;b=b>>1;}
if(cnt>=D)
return true;
else
return false;
}
bool Is2(int top,int b)
{
for(int i=0; i<top; ++i)
if(!Is(ans[i],b))
return false;
return true;
}

void solve()
{int cnt=1;
int count=1;
while(cnt<N)
{
if(Is2(cnt,count))
{ans[cnt]=count; ++cnt;}
count++;
}
}

int main()
{
ifstream fin("hamming.in");
ofstream fout("hamming.out");
fin>>N>>B>>D;
solve();
for(int i=0,j=1; i<N-1; ++i,++j)
{ if(j<10)
fout<<ans[i]<<' ';
else
{fout<<ans[i]<<endl; j=0;}
}
fout<<ans[N-1]<<endl;
return 0;
}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: