usaco 2.1.5 Hamming Codes

来源:互联网 发布:二维数组的定义 编辑:程序博客网 时间:2024/05/16 12:29

被抛弃的少猫爆人品.

这题中午看的时候只会感叹"毛啊!"但是接受了中间那个数据基本是在搞笑以后,渐渐也觉得有爱起来了呢.

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 0100Bit 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 7682 85 97 102 120 127
用到了神一样的位运算.当初67神用的是pascal, 我n久以前研习的时候用c码了一遍,这次直接就用上了,so happy~~

/*ID: wtff0411PROG: hammingLANG: C++*/#include <iostream>#include <fstream>#include <string>#include <cstring>#include <vector>#include <cmath>#include <queue>#include <algorithm>using namespace std;int bitc(int a){     int x=a;     x = (x&0x55555555) + ((x>>1)&0x55555555);      x = (x&0x33333333) + ((x>>2)&0x33333333); //00110011001100     x = (x&0x0F0F0F0F) + ((x>>4)&0x0F0F0F0F);      x = (x&0x00FF00FF) + ((x>>8)&0x00FF00FF);      x = (x&0x0000FFFF) + ((x>>16)&0x0000FFFF);     return x;} int main(){    freopen("hamming.in","r",stdin);    freopen("hamming.out","w",stdout);    int n,b,d;    cin>>n>>b>>d;    int num=1;    int i,j;    int m;    m=pow(2.0,(double)b);    cout<<0<<" ";    int save[100];    memset(save,0,sizeof(save));    for(i=1;i<m,num<n;i++)    {        int flag=0;        for(j=0;j<num;j++)            if(bitc(i^save[j])<d)            {                flag=1;                break;            }        if(flag==0)        {            save[num]=i;            cout<<i;            if(num%10==9)            cout<<endl;            else if(num==n-1)            {cout<<endl;            break;}            else            cout<<" ";             num++;        }    }        //system("pause");    return 0;}

我会随便吐槽这神一样的输出么?一开始还觉得自己已经想的很全了,拿衣服啊!


话说果然只有被抛弃时才会发粪涂墙.唉.

可是,我果然还是更希望堕落一点...

原创粉丝点击