考试周终于过去了一半,可以继续写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 = ; 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[ << maxn];
result[] = ;
int n_ptr = ;
for(int i = ; i < ( << b); i++) {
if(n_ptr > n) {
break;
}
bool can_add = true;
for(int j = ; 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 = ; i < n - ; i++) {
cout << result[i];
if((i + ) % ) {
cout << " ";
} else {
cout << endl;
}
}
cout << result[n - ] << endl;
} int count(int a, int b) {
int result = ;
for(int i = ; i < maxn; i++) {
if(((a>>i) & ) != ((b>>i) & )) {
result++;
}
}
return result;
}

USACO hamming的更多相关文章

  1. USACO Hamming Codes DFS 构造

    我还是用了很朴素的暴力匹配A了这题,不得不感叹USACO时间放的好宽... /* ID: wushuai2 PROG: hamming LANG: C++ */ //#pragma comment(l ...

  2. USACO Hamming Codes

    题目大意:求n个两两hamming距离大于等于d的序列,每个元素是一个b bit的数 思路:仍然暴力大法好 /*{ ID:a4298442 PROB:hamming LANG:C++ } */ #in ...

  3. 【USACO 2.1】Hamming Codes

    /* TASK: hamming LANG: C++ URL:http://train.usaco.org/usacoprob2?a=5FomsUyB0cP&S=hamming SOLVE: ...

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

    hamming解题报告----------------------------------------------------------------------------------------- ...

  5. USACO 2.1 海明码 Hamming Codes (模拟+位运算+黑科技__builtin_popcount(n))

    题目描述 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个单位 ...

  6. USACO Section 2.1: Hamming Codes

    挺简单的一道题 /* ID: yingzho1 LANG: C++ TASK: hamming */ #include <iostream> #include <fstream> ...

  7. USACO 2.1 Hamming Codes

    Hamming CodesRob Kolstad Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of ...

  8. 洛谷P1461 海明码 Hamming Codes

    P1461 海明码 Hamming Codes 98通过 120提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 给出 N,B 和 ...

  9. P1461 海明码 Hamming Codes

    题目描述 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个单位 ...

随机推荐

  1. 使用Nginx实现Tomcat集群负载均衡

    概述 要解决的问题 环境准备以及问题解决思路 配置 测试 小结 一.概述 使用Nginx主要是来解决高并发情况下的负载均衡问题. 二.要解决的问题 1.最主要是负载均衡请求分发. 2.文件上传功能,只 ...

  2. 1.javascript节点的操作 创建、添加、移除、移动、复制、插入(修改)

    (1)创建新节点 createDocumentFragment() //创建一个DOM片段 createElement() //创建一个具体的元素 createTextNode() //创建一个文本节 ...

  3. ASP.Net WebAPI与Ajax进行跨域数据交互时Cookies数据的传递

    前言 最近公司项目进行架构调整,由原来的三层架构改进升级到微服务架构(准确的说是服务化,还没完全做到微的程度,颗粒度没那么细),遵循RESTFull规范,使前后端完全分离,实现大前端思想.由于是初次尝 ...

  4. 弹出输入内容prompt

    <script> window.onload = function(){ var oBtn = document.getElementById( "btn" ); oB ...

  5. 计算facebook sdk需要的key hashes

    1. 下载openssl,在C盘创建openssl文件夹,解压下载的zip到openssl文件夹. 2. 找到debug.keystore. windows下在C:\Users\用户名\.androi ...

  6. AngularJS操作DOM——angular.element

    addClass()-为每个匹配的元素添加指定的样式类名 after()-在匹配元素集合中的每个元素后面插入参数所指定的内容,作为其兄弟节点 append()-在每个匹配元素里面的末尾处插入参数内容a ...

  7. CountDownLatch, CyclicBarrier and Semaphore

    Reference: [1] http://shazsterblog.blogspot.co.uk/2011/12/comparison-of-countdownlatch.html CountDow ...

  8. 浏览器兼容性--new Date

    ie浏览器下new Date("2013/04")与new Date("2016-04")会报错: //将201601格式的字符串转为Date对象,月份从0开始 ...

  9. React源码学习——ReactClass

    前言 之前一直在使用react做开发,但是对其内部的工作机制却一点儿都不了解,说白了就是一直在套api,毫无成就感.趁最近比较闲,对源码做了一番研究,并通过博客的方式做一些记录. 进入正题 通过编写自 ...

  10. JVM、GC与HashMap

    阿里巴巴突然来了个面试邀请电话,问了些java底层的东西,不知所措,所以专门花了些时间做了下学习,顺便记录下,好记性不如烂笔头. 一.对JAVA的垃圾回收机制(GC)的理解 不同于C/C++需要手工释 ...