zoukankan      html  css  js  c++  java
  • 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)),不敢写,后来上网看了解析,居然就是搜索,还是最裸的那种。。。有点想不明白,不过也提醒我,撑死胆大的,饿死胆小的。代码如下
     1 /*
     2 ID:fffgrdc1
     3 PROB:hamming
     4 LANG:C++
     5 */
     6 #include<cstdio>
     7 #include<iostream>
     8 #include<cstdlib>
     9 using namespace std;
    10 int n,m,d,cnt=1;
    11 int topnum=1;
    12 int ans[65]={0};
    13 bool check(int x,int y)
    14 {
    15     x=x^y;int tot=0;
    16     while(x)
    17     {
    18         if(x&1)tot++;
    19         x>>=1;
    20     }
    21     return tot>=d;
    22 }
    23 void dfs(int x)
    24 {
    25     if(cnt==n)
    26     {
    27         int num=0;
    28         for(int i=1;i<=cnt;i++)
    29         {
    30             num++;
    31             if(num==1)
    32                 printf("%d",ans[i]);
    33             else printf(" %d",ans[i]);
    34             if(num==10)
    35             {
    36                 printf("
    ");
    37                 num=0;
    38             }
    39         }
    40         if(num)printf("
    ");
    41         exit(0);
    42     }
    43     if(x>topnum)return;
    44     int flag=1;
    45     for(int i=1;i<=cnt;i++)
    46     {
    47         if(!check(ans[i],x))
    48         {
    49             flag=0;
    50             break;
    51         }
    52     }
    53     if(flag)
    54     {
    55         ans[++cnt]=x;
    56         dfs(x+1);
    57         cnt--;
    58     }
    59     dfs(x+1);
    60 
    61 }
    62 int main()
    63 {
    64     freopen("hamming.in","r",stdin);
    65     freopen("hamming.out","w",stdout);
    66     scanf("%d%d%d",&n,&m,&d);
    67     while(m--)topnum<<=1;
    68     topnum--;
    69     //printf("%d
    ",topnum);
    70     dfs(1);
    71     return 0;
    72 }
  • 相关阅读:
    JS的数据类型(包含:7种数据类型的介绍、数据类型的转换、数据类型的判断)
    使用终端改变MAC默认截图存放地址
    Homebrew的安装及使用
    CSS多列布局(实例)
    CSS中元素各种居中方法(思维导图)
    用CSS伪类制作一个不断旋转的八卦图?
    DOSBOX的简单使用
    5分钟读懂JavaScript预编译流程
    基于栈虚拟机和基于寄存器虚拟机的比较
    js执行可视化
  • 原文地址:https://www.cnblogs.com/xuwangzihao/p/5010583.html
Copyright © 2011-2022 走看看