USACO 2.1.5 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 codewordsis at least Hamming distance of D (1 <= D <= 7) away from each ofthe other codewords. The Hamming distance between a pair of codewords isthe number of binary bits that differ in their binary notation. Considerthe two codewords 0x554 and 0x234 and their differences (0x554 means thehexadecimal 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 multiplesolutions, your program should output the solution which, if interpretedas 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

题意:给出 N,B 和 D:找出 N 个编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个单位的“海明距离”(1 <= D <= 7)。“海明距离”是指对于两个编码,他们的二进制表示法中的不同二进制位的数目,注意输出时要10个十进制数字一行。

思路: 第一步,从0~2^B-1中找出与零的海明距离>=D的所有数存储在一个一维数组中(0一定在输出结果中,所以其他的输出数据和0的海明距离一定>=D)。第二步,要遍历这个一维数组,再定义一个数组,找出其他满足条件的数字填入到这个数组中。比如在给出的样例中,已找到了0,7,25,30在判断32是否该输出时,计算0与32的海明距离不符合要求,那后面的7,25,30就都不用计算了。

源代码:

/*
ID: supersnow0622
PROG: hamming
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include<memory.h>
using namespace std;
int N,B,D;
int have1[8]={0,1,3,7,15,31,63,127};
int arr[300];
int convert(int n)
{
   int count=0;
   while(n!=0)
   {
    if(n%2==1)
      count++;
    n/=2;
   }
return count;
}
int main() {
    ofstream fout ("hamming.out");
    ifstream fin ("hamming.in");
    cin>>N>>B>>D;
    if(D==1)
    {
          cout<<0;
      for(int i=1;i<N;i++)
      {
         if(i%10==0)
           cout<<endl;
         else cout<<" ";
         cout<<i;
      }
      cout<<endl;
    return 0;
    }
    int start=have1[D],temp;
    int num=0;
    int result[300],n=1;
    memset(result,0,sizeof(result));
    result[0]=0;result[1]=start;
    for(int i=start;i<1<<B;i++)
       if(convert(i)>=D)
         arr[num++]=i;
          int judge;
    for(int i=1;i<num;i++)
      {
        judge=true;
         for(int j=1;j<=n;j++)
           {
             temp=result[j]^arr[i];
             if(convert(temp)<D)
              {
                judge=false;
                break;
              }
           }
           if(judge)
             {
              result[++n]=arr[i];
             }
           if(n==N-1)
             break;
      }
          cout<<0;
      for(int i=1;i<N;i++)
      {
         if(i%10==0)
           cout<<endl;
         else cout<<" ";
         cout<<result[i];
      }

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值