USACO hamming

考试周终于过去了一半,可以继续写USACO了。

先来看一下题目吧。

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: njuwz151
TASK: hamming
LANG: C++
*/
#include <iostream>
#include <cstdio>

using namespace std;

const int maxn = 9;

int count(int a, int b);

int main() {
    freopen("hamming.in", "r", stdin);
    freopen("hamming.out", "w", stdout);

    int n, b, d;
    cin >> n >> b >> d;

    int result[1 << maxn];
    result[0] = 0;
    int n_ptr = 1;
    for(int i = 1; i < (1 << b); i++) {
        if(n_ptr > n) {
            break;
        }
        bool can_add = true;
        for(int j = 0; j < n_ptr; j++) {
            if(count(i, result[j]) < d) {
                can_add = false;
                break;
            }
        }
        if(can_add) {
            result[n_ptr] = i;
            n_ptr++;
        }
    }
    for(int i = 0; i < n - 1; i++) {
        cout << result[i];
        if((i + 1) % 10) {
            cout << " ";
        } else {
            cout << endl;
        }
    }
    cout << result[n - 1] << endl;
} 

int count(int a, int b) {
    int result = 0;
    for(int i = 0; i < maxn; i++) {
        if(((a>>i) & 1) != ((b>>i) & 1)) {
            result++;
        }
    }
    return result;
}
时间: 2024-10-08 07:14:16

USACO hamming的相关文章

USACO hamming 继续暴搜

USER: Kevin Samuel [kevin_s1] TASK: hamming LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.003 secs, 3504 KB] Test 2: TEST OK [0.005 secs, 3504 KB] Test 3: TEST OK [0.008 secs, 3504 KB] Test 4: TEST OK [0.008 secs, 3504 KB] Test 5

USACO Hamming Codes DFS 构造

我还是用了很朴素的暴力匹配A了这题,不得不感叹USACO时间放的好宽... /* ID: wushuai2 PROG: hamming LANG: C++ */ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring>

usaco Hamming Codes

题目很简单,找出N个二进制下长度为B的,且两两之间二进制位上至少有D位不同 暴力枚举就好了 /* ID: modengd1 PROG: hamming LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> using namespace std; int sta[64]; bool slove(int sta[64],int N,int B,int D,int deep) { if

Section 2.1.Hamming Codes (hamming)

//题目的链接http://www.nocow.cn/index.php/Translate:USACO/hamming/*[位运算]+[异或操作]*//* ID: zhangsh35 PROG: hamming LANG: C++ */ #include<iostream> #include<math.h> #include<stdio.h> using namespace std; const int MAXN=64; int ham[MAXN]; int tot,

【USACO 2.1】Hamming Codes

/* TASK: hamming LANG: C++ URL:http://train.usaco.org/usacoprob2?a=5FomsUyB0cP&S=hamming SOLVE: 找粗一个值最小的n个元素的集合,每个元素都是不超过m位二进制的数,且两两之间二进制位不同的位不小于d. dfs,枚举每一个数,枚举范围:(前一个数,1<<m),每次进入dfs都判断一下当前集合是否满足两两距离不小于d. */ #include<cstdio> int n,m,d; in

USACO Section2.1 Hamming Codes 解题报告 【icedream61】

hamming解题报告------------------------------------------------------------------------------------------------------------------------------------------------[题目] 找出N个二进制数,每个数有B位,使得各数两两之间“海明距离”至少为D.(若有多组解,输出字典序最小的.) 海明距离是指:两个二进制数不同二进制位的个数.[数据范围] 1<=N<=

USACO Section 2.1: Hamming Codes

dfs简单题 1 /* 2 ID: yingzho2 3 PROG: hamming 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <string> 9 #include <map> 10 #include <vector> 11 #include <set> 12 #include <algorithm> 13 #includ

[USACO][枚举]Hamming Code

题意: 给出N,B,D,要求输出N个十进制数字,他们之间的Hamming距离在长度为B位的时候都等于D. 思路: 感觉图论就是一个,允许我们记忆化的边权好工具!(废话用edges存边了还不是当然的) 代码: /* ID :ggy_7781 TASK :hamming LANG :C++11 */ #include <bits/stdc++.h> #define maxB 8 #define maxN 64 #define maxD 7 using namespace std; int N,B,

USACO 之 Section 2.1 (已解决)

The Castle: /* 搜索 1A*/ 1 /* 2 ID: Jming 3 PROG: castle 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <sstream> 9 #include <cstdlib> 10 #include <cstdio> 11 #include <cstddef> 12 #include <ite