zoukankan      html  css  js  c++  java
  • USACO2.1.5Hamming 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
    题解:赤果果的枚举+位运算啊。枚举范围为 [0,2^B-1],先对两个编码进行异或运算得到一个新数字,然后对数字进行与运算和移位运算把其中的1的个数计算出来,再判断Hamming距离是否大于D,如果是则是符合要求的(必须与其他所有的数相比,Hamming距离都符合要求,这个数才正确)。
    View Code
     1 /*
     2 ID:spcjv51
     3 PROG:hamming
     4 LANG:C
     5 */
     6 #include<stdio.h>
     7 int n,a[500];
     8 int distance(int x)
     9 {
    10     int ans;
    11     ans=0;
    12     while(x)
    13     {
    14         ans+=x&1;
    15         x>>=1;
    16     }
    17     return ans;
    18 }
    19 void print()
    20 {
    21     int i;
    22     for(i=1;i<n;i++)
    23     {
    24         if(i%10==0) printf("%d\n",a[i]);
    25         else
    26         printf("%d ",a[i]);
    27     }
    28     printf("%d\n",a[i]);
    29 }
    30 int main(void)
    31 {
    32     freopen("hamming.in","r",stdin);
    33     freopen("hamming.out","w",stdout);
    34     int b,d,ans,i,j,k,x,y;
    35     scanf("%d%d%d",&n,&b,&d);
    36     for(i=0; i<(1<<b)-1; i++)
    37     {
    38         ans=1;
    39         a[ans]=i;
    40         for(j=i+1; j<(1<<b); j++)
    41         {
    42             x=i^j;
    43             if(distance(x)>=d)
    44             {
    45                 for(k=1; k<=ans; k++)
    46                 {
    47                     y=j^a[k];
    48                     if(distance(y)<d)
    49                         break;
    50                 }
    51                 if(k==ans+1)
    52                 {
    53                     ans++;
    54                     a[ans]=j;
    55                 }
    56             }
    57             if(ans==n) break;
    58 
    59         }
    60         if(ans==n)
    61         {
    62             print();
    63             break;
    64         }
    65     }
    66     return 0;
    67 }
    
    
  • 相关阅读:
    win7系统内网共享打印机设置
    VS中无法打开Qt资源文件qrc
    EF开发中EntityFramework在web.config中的配置问题
    【转】为什么你的硬盘容易坏?因为它转得实在是太快了
    AutoCAD批量导出点坐标
    【读书】《当我跑步时,我谈些什么》书评:我跑步时,只是跑着
    【C/C++】How to execute a particular function before main() in C?
    【gdb】A brief introduction on how to use gdb
    【Valgrind】How to check if we reading uninitialized memory in 10 min
    【Valgrind】How to check buffer overflow/underflow in 10 mins
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2891164.html
Copyright © 2011-2022 走看看